


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using SubSonic.DataProviders;
using SubSonic.Extensions;
using System.Linq.Expressions;
using SubSonic.Schema;
using System.Collections;
using SubSonic;
using SubSonic.Repository;
using System.ComponentModel;
using System.Data.Common;
using System.Data.SqlClient;


namespace Domain.Model
{
    
    
    /// <summary>
    /// A class which represents the Caixa table in the BelDecor Database.
    /// </summary>
    public partial class Caixa: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<Caixa> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<Caixa>(new Domain.Model.BelDecorDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<Caixa> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(Caixa item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                Caixa item=new Caixa();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<Caixa> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Domain.Model.BelDecorDB _db;
        public Caixa(string connectionString, string providerName) {

            _db=new Domain.Model.BelDecorDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                Caixa.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<Caixa>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public Caixa(){
             _db=new Domain.Model.BelDecorDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public Caixa(Expression<Func<Caixa, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<Caixa> GetRepo(string connectionString, string providerName){
            Domain.Model.BelDecorDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Domain.Model.BelDecorDB();
            }else{
                db=new Domain.Model.BelDecorDB(connectionString, providerName);
            }
            IRepository<Caixa> _repo;
            
            if(db.TestMode){
                Caixa.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<Caixa>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<Caixa> GetRepo(){
            return GetRepo("","");
        }
        
        public static Caixa SingleOrDefault(Expression<Func<Caixa, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            Caixa single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static Caixa SingleOrDefault(Expression<Func<Caixa, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            Caixa single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<Caixa, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<Caixa, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<Caixa> Find(Expression<Func<Caixa, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<Caixa> Find(Expression<Func<Caixa, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<Caixa> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<Caixa> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<Caixa> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<Caixa> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<Caixa> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<Caixa> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "Id_Caixa";
        }

        public object KeyValue()
        {
            return this.Id_Caixa;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.Numero_Caixa.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(Caixa)){
                Caixa compare=(Caixa)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        //public override int GetHashCode() {
        //    return this.Id_Caixa;
        //}
        
        public string DescriptorValue()
        {
                            return this.Numero_Caixa.ToString();
                    }

        public string DescriptorColumn() {
            return "Numero_Caixa";
        }
        public static string GetKeyColumn()
        {
            return "Id_Caixa";
        }        
        public static string GetDescriptorColumn()
        {
            return "Numero_Caixa";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _Id_Caixa;
        public int Id_Caixa
        {
            get { return _Id_Caixa; }
            set
            {
                if(_Id_Caixa!=value){
                    _Id_Caixa=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Id_Caixa");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        int _Numero_Caixa;
        public int Numero_Caixa
        {
            get { return _Numero_Caixa; }
            set
            {
                if(_Numero_Caixa!=value){
                    _Numero_Caixa=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Numero_Caixa");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        DateTime _Data_Abertura;
        public DateTime Data_Abertura
        {
            get { return _Data_Abertura; }
            set
            {
                if(_Data_Abertura!=value){
                    _Data_Abertura=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Data_Abertura");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        DateTime _Data_Fechamento;
        public DateTime Data_Fechamento
        {
            get { return _Data_Fechamento; }
            set
            {
                if(_Data_Fechamento!=value){
                    _Data_Fechamento=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Data_Fechamento");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        int _valor;
        public int valor
        {
            get { return _valor; }
            set
            {
                if(_valor!=value){
                    _valor=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="valor");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
		
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<Caixa, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }

		public void GeraAvaliacoes()
        {
            DbCommand cmd = _db.DataProvider.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_tempoAvaliacao";
            cmd.Connection = _db.DataProvider.CurrentSharedConnection;

            int r = cmd.ExecuteNonQuery();

            cmd.Dispose();
        }
        
		public void ReativaEmails()
        {
            DbCommand cmd = _db.DataProvider.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_reativaEmails";
            cmd.Connection = _db.DataProvider.CurrentSharedConnection;

            int r = cmd.ExecuteNonQuery();

            cmd.Dispose();
        }

    } 
    
    
    /// <summary>
    /// A class which represents the Cliente table in the BelDecor Database.
    /// </summary>
    public partial class Cliente: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<Cliente> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<Cliente>(new Domain.Model.BelDecorDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<Cliente> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(Cliente item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                Cliente item=new Cliente();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<Cliente> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Domain.Model.BelDecorDB _db;
        public Cliente(string connectionString, string providerName) {

            _db=new Domain.Model.BelDecorDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                Cliente.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<Cliente>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public Cliente(){
             _db=new Domain.Model.BelDecorDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public Cliente(Expression<Func<Cliente, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<Cliente> GetRepo(string connectionString, string providerName){
            Domain.Model.BelDecorDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Domain.Model.BelDecorDB();
            }else{
                db=new Domain.Model.BelDecorDB(connectionString, providerName);
            }
            IRepository<Cliente> _repo;
            
            if(db.TestMode){
                Cliente.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<Cliente>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<Cliente> GetRepo(){
            return GetRepo("","");
        }
        
        public static Cliente SingleOrDefault(Expression<Func<Cliente, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            Cliente single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static Cliente SingleOrDefault(Expression<Func<Cliente, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            Cliente single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<Cliente, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<Cliente, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<Cliente> Find(Expression<Func<Cliente, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<Cliente> Find(Expression<Func<Cliente, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<Cliente> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<Cliente> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<Cliente> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<Cliente> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<Cliente> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<Cliente> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "id";
        }

        public object KeyValue()
        {
            return this.id;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.dataCadastro.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(Cliente)){
                Cliente compare=(Cliente)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        //public override int GetHashCode() {
        //    return this.id;
        //}
        
        public string DescriptorValue()
        {
                            return this.dataCadastro.ToString();
                    }

        public string DescriptorColumn() {
            return "dataCadastro";
        }
        public static string GetKeyColumn()
        {
            return "id";
        }        
        public static string GetDescriptorColumn()
        {
            return "dataCadastro";
        }
        
        #region ' Foreign Keys '
        public IQueryable<Pedido> Pedidos
        {
            get
            {
                
                  var repo=Domain.Model.Pedido.GetRepo();
                  return from items in repo.GetAll()
                       where items.IdCliente == _id
                       select items;
            }
        }

        public IQueryable<Pessoa> Pessoas
        {
            get
            {
                
                  var repo=Domain.Model.Pessoa.GetRepo();
                  return from items in repo.GetAll()
                       where items.idPessoa == _IdPessoa
                       select items;
            }
        }

        #endregion
        

        int _id;
        public int id
        {
            get { return _id; }
            set
            {
                if(_id!=value){
                    _id=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        DateTime? _dataCadastro;
        public DateTime? dataCadastro
        {
            get { return _dataCadastro; }
            set
            {
                if(_dataCadastro!=value){
                    _dataCadastro=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="dataCadastro");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        int? _IdPessoa;
        public int? IdPessoa
        {
            get { return _IdPessoa; }
            set
            {
                if(_IdPessoa!=value){
                    _IdPessoa=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="IdPessoa");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
		
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<Cliente, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }

		public void GeraAvaliacoes()
        {
            DbCommand cmd = _db.DataProvider.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_tempoAvaliacao";
            cmd.Connection = _db.DataProvider.CurrentSharedConnection;

            int r = cmd.ExecuteNonQuery();

            cmd.Dispose();
        }
        
		public void ReativaEmails()
        {
            DbCommand cmd = _db.DataProvider.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_reativaEmails";
            cmd.Connection = _db.DataProvider.CurrentSharedConnection;

            int r = cmd.ExecuteNonQuery();

            cmd.Dispose();
        }

    } 
    
    
    /// <summary>
    /// A class which represents the ContaPagar table in the BelDecor Database.
    /// </summary>
    public partial class ContaPagar: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<ContaPagar> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<ContaPagar>(new Domain.Model.BelDecorDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<ContaPagar> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(ContaPagar item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                ContaPagar item=new ContaPagar();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<ContaPagar> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Domain.Model.BelDecorDB _db;
        public ContaPagar(string connectionString, string providerName) {

            _db=new Domain.Model.BelDecorDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                ContaPagar.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ContaPagar>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public ContaPagar(){
             _db=new Domain.Model.BelDecorDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public ContaPagar(Expression<Func<ContaPagar, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<ContaPagar> GetRepo(string connectionString, string providerName){
            Domain.Model.BelDecorDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Domain.Model.BelDecorDB();
            }else{
                db=new Domain.Model.BelDecorDB(connectionString, providerName);
            }
            IRepository<ContaPagar> _repo;
            
            if(db.TestMode){
                ContaPagar.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ContaPagar>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<ContaPagar> GetRepo(){
            return GetRepo("","");
        }
        
        public static ContaPagar SingleOrDefault(Expression<Func<ContaPagar, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            ContaPagar single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static ContaPagar SingleOrDefault(Expression<Func<ContaPagar, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            ContaPagar single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<ContaPagar, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<ContaPagar, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<ContaPagar> Find(Expression<Func<ContaPagar, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<ContaPagar> Find(Expression<Func<ContaPagar, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<ContaPagar> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<ContaPagar> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<ContaPagar> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<ContaPagar> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<ContaPagar> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<ContaPagar> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "Id_ContaPagar";
        }

        public object KeyValue()
        {
            return this.Id_ContaPagar;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.Favorecido_Cedente.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(ContaPagar)){
                ContaPagar compare=(ContaPagar)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        //public override int GetHashCode() {
        //    return this.Id_ContaPagar;
        //}
        
        public string DescriptorValue()
        {
                            return this.Favorecido_Cedente.ToString();
                    }

        public string DescriptorColumn() {
            return "Favorecido_Cedente";
        }
        public static string GetKeyColumn()
        {
            return "Id_ContaPagar";
        }        
        public static string GetDescriptorColumn()
        {
            return "Favorecido_Cedente";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _Id_ContaPagar;
        public int Id_ContaPagar
        {
            get { return _Id_ContaPagar; }
            set
            {
                if(_Id_ContaPagar!=value){
                    _Id_ContaPagar=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Id_ContaPagar");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        int? _Numero_Conta;
        public int? Numero_Conta
        {
            get { return _Numero_Conta; }
            set
            {
                if(_Numero_Conta!=value){
                    _Numero_Conta=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Numero_Conta");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _Favorecido_Cedente;
        public string Favorecido_Cedente
        {
            get { return _Favorecido_Cedente; }
            set
            {
                if(_Favorecido_Cedente!=value){
                    _Favorecido_Cedente=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Favorecido_Cedente");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        DateTime? _Data_Vencimento;
        public DateTime? Data_Vencimento
        {
            get { return _Data_Vencimento; }
            set
            {
                if(_Data_Vencimento!=value){
                    _Data_Vencimento=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Data_Vencimento");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        decimal? _Valor_Conta;
        public decimal? Valor_Conta
        {
            get { return _Valor_Conta; }
            set
            {
                if(_Valor_Conta!=value){
                    _Valor_Conta=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Valor_Conta");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _Observacoes;
        public string Observacoes
        {
            get { return _Observacoes; }
            set
            {
                if(_Observacoes!=value){
                    _Observacoes=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Observacoes");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        bool? _PagamentoEfetuado;
        public bool? PagamentoEfetuado
        {
            get { return _PagamentoEfetuado; }
            set
            {
                if(_PagamentoEfetuado!=value){
                    _PagamentoEfetuado=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="PagamentoEfetuado");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        int? _idTipo;
        public int? idTipo
        {
            get { return _idTipo; }
            set
            {
                if(_idTipo!=value){
                    _idTipo=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="idTipo");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
		
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<ContaPagar, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }

		public void GeraAvaliacoes()
        {
            DbCommand cmd = _db.DataProvider.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_tempoAvaliacao";
            cmd.Connection = _db.DataProvider.CurrentSharedConnection;

            int r = cmd.ExecuteNonQuery();

            cmd.Dispose();
        }
        
		public void ReativaEmails()
        {
            DbCommand cmd = _db.DataProvider.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_reativaEmails";
            cmd.Connection = _db.DataProvider.CurrentSharedConnection;

            int r = cmd.ExecuteNonQuery();

            cmd.Dispose();
        }

    } 
    
    
    /// <summary>
    /// A class which represents the ContaReceber table in the BelDecor Database.
    /// </summary>
    public partial class ContaReceber: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<ContaReceber> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<ContaReceber>(new Domain.Model.BelDecorDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<ContaReceber> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(ContaReceber item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                ContaReceber item=new ContaReceber();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<ContaReceber> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Domain.Model.BelDecorDB _db;
        public ContaReceber(string connectionString, string providerName) {

            _db=new Domain.Model.BelDecorDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                ContaReceber.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ContaReceber>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public ContaReceber(){
             _db=new Domain.Model.BelDecorDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public ContaReceber(Expression<Func<ContaReceber, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<ContaReceber> GetRepo(string connectionString, string providerName){
            Domain.Model.BelDecorDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Domain.Model.BelDecorDB();
            }else{
                db=new Domain.Model.BelDecorDB(connectionString, providerName);
            }
            IRepository<ContaReceber> _repo;
            
            if(db.TestMode){
                ContaReceber.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ContaReceber>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<ContaReceber> GetRepo(){
            return GetRepo("","");
        }
        
        public static ContaReceber SingleOrDefault(Expression<Func<ContaReceber, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            ContaReceber single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static ContaReceber SingleOrDefault(Expression<Func<ContaReceber, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            ContaReceber single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<ContaReceber, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<ContaReceber, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<ContaReceber> Find(Expression<Func<ContaReceber, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<ContaReceber> Find(Expression<Func<ContaReceber, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<ContaReceber> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<ContaReceber> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<ContaReceber> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<ContaReceber> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<ContaReceber> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<ContaReceber> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "Id_Conta";
        }

        public object KeyValue()
        {
            return this.Id_Conta;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.Forma_Recebimento.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(ContaReceber)){
                ContaReceber compare=(ContaReceber)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        //public override int GetHashCode() {
        //    return this.Id_Conta;
        //}
        
        public string DescriptorValue()
        {
                            return this.Forma_Recebimento.ToString();
                    }

        public string DescriptorColumn() {
            return "Forma_Recebimento";
        }
        public static string GetKeyColumn()
        {
            return "Id_Conta";
        }        
        public static string GetDescriptorColumn()
        {
            return "Forma_Recebimento";
        }
        
        #region ' Foreign Keys '
        public IQueryable<Pedido> Pedidos
        {
            get
            {
                
                  var repo=Domain.Model.Pedido.GetRepo();
                  return from items in repo.GetAll()
                       where items.Id_Pedido == _Id_Pedido
                       select items;
            }
        }

        #endregion
        

        int _Id_Conta;
        public int Id_Conta
        {
            get { return _Id_Conta; }
            set
            {
                if(_Id_Conta!=value){
                    _Id_Conta=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Id_Conta");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        int? _Id_Pedido;
        public int? Id_Pedido
        {
            get { return _Id_Pedido; }
            set
            {
                if(_Id_Pedido!=value){
                    _Id_Pedido=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Id_Pedido");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _Forma_Recebimento;
        public string Forma_Recebimento
        {
            get { return _Forma_Recebimento; }
            set
            {
                if(_Forma_Recebimento!=value){
                    _Forma_Recebimento=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Forma_Recebimento");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        decimal? _Valor_Recebimento;
        public decimal? Valor_Recebimento
        {
            get { return _Valor_Recebimento; }
            set
            {
                if(_Valor_Recebimento!=value){
                    _Valor_Recebimento=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Valor_Recebimento");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        DateTime? _data_recebimento;
        public DateTime? data_recebimento
        {
            get { return _data_recebimento; }
            set
            {
                if(_data_recebimento!=value){
                    _data_recebimento=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="data_recebimento");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
		
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<ContaReceber, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }

		public void GeraAvaliacoes()
        {
            DbCommand cmd = _db.DataProvider.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_tempoAvaliacao";
            cmd.Connection = _db.DataProvider.CurrentSharedConnection;

            int r = cmd.ExecuteNonQuery();

            cmd.Dispose();
        }
        
		public void ReativaEmails()
        {
            DbCommand cmd = _db.DataProvider.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_reativaEmails";
            cmd.Connection = _db.DataProvider.CurrentSharedConnection;

            int r = cmd.ExecuteNonQuery();

            cmd.Dispose();
        }

    } 
    
    
    /// <summary>
    /// A class which represents the Endereco table in the BelDecor Database.
    /// </summary>
    public partial class Endereco: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<Endereco> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<Endereco>(new Domain.Model.BelDecorDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<Endereco> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(Endereco item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                Endereco item=new Endereco();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<Endereco> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Domain.Model.BelDecorDB _db;
        public Endereco(string connectionString, string providerName) {

            _db=new Domain.Model.BelDecorDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                Endereco.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<Endereco>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public Endereco(){
             _db=new Domain.Model.BelDecorDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public Endereco(Expression<Func<Endereco, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<Endereco> GetRepo(string connectionString, string providerName){
            Domain.Model.BelDecorDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Domain.Model.BelDecorDB();
            }else{
                db=new Domain.Model.BelDecorDB(connectionString, providerName);
            }
            IRepository<Endereco> _repo;
            
            if(db.TestMode){
                Endereco.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<Endereco>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<Endereco> GetRepo(){
            return GetRepo("","");
        }
        
        public static Endereco SingleOrDefault(Expression<Func<Endereco, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            Endereco single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static Endereco SingleOrDefault(Expression<Func<Endereco, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            Endereco single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<Endereco, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<Endereco, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<Endereco> Find(Expression<Func<Endereco, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<Endereco> Find(Expression<Func<Endereco, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<Endereco> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<Endereco> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<Endereco> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<Endereco> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<Endereco> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<Endereco> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "IdEndereco";
        }

        public object KeyValue()
        {
            return this.IdEndereco;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.logradouro.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(Endereco)){
                Endereco compare=(Endereco)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        //public override int GetHashCode() {
        //    return this.IdEndereco;
        //}
        
        public string DescriptorValue()
        {
                            return this.logradouro.ToString();
                    }

        public string DescriptorColumn() {
            return "logradouro";
        }
        public static string GetKeyColumn()
        {
            return "IdEndereco";
        }        
        public static string GetDescriptorColumn()
        {
            return "logradouro";
        }
        
        #region ' Foreign Keys '
        public IQueryable<Fornecedor> Fornecedors
        {
            get
            {
                
                  var repo=Domain.Model.Fornecedor.GetRepo();
                  return from items in repo.GetAll()
                       where items.IdEndereco == _IdEndereco
                       select items;
            }
        }

        public IQueryable<Pessoa> Pessoas
        {
            get
            {
                
                  var repo=Domain.Model.Pessoa.GetRepo();
                  return from items in repo.GetAll()
                       where items.idEndereco == _IdEndereco
                       select items;
            }
        }

        #endregion
        

        int _IdEndereco;
        public int IdEndereco
        {
            get { return _IdEndereco; }
            set
            {
                if(_IdEndereco!=value){
                    _IdEndereco=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="IdEndereco");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _logradouro;
        public string logradouro
        {
            get { return _logradouro; }
            set
            {
                if(_logradouro!=value){
                    _logradouro=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="logradouro");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _numero;
        public string numero
        {
            get { return _numero; }
            set
            {
                if(_numero!=value){
                    _numero=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="numero");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _complemento;
        public string complemento
        {
            get { return _complemento; }
            set
            {
                if(_complemento!=value){
                    _complemento=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="complemento");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _bairro;
        public string bairro
        {
            get { return _bairro; }
            set
            {
                if(_bairro!=value){
                    _bairro=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="bairro");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _cidade;
        public string cidade
        {
            get { return _cidade; }
            set
            {
                if(_cidade!=value){
                    _cidade=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="cidade");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _estado;
        public string estado
        {
            get { return _estado; }
            set
            {
                if(_estado!=value){
                    _estado=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="estado");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _cep;
        public string cep
        {
            get { return _cep; }
            set
            {
                if(_cep!=value){
                    _cep=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="cep");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _pais;
        public string pais
        {
            get { return _pais; }
            set
            {
                if(_pais!=value){
                    _pais=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="pais");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
		
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<Endereco, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }

		public void GeraAvaliacoes()
        {
            DbCommand cmd = _db.DataProvider.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_tempoAvaliacao";
            cmd.Connection = _db.DataProvider.CurrentSharedConnection;

            int r = cmd.ExecuteNonQuery();

            cmd.Dispose();
        }
        
		public void ReativaEmails()
        {
            DbCommand cmd = _db.DataProvider.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_reativaEmails";
            cmd.Connection = _db.DataProvider.CurrentSharedConnection;

            int r = cmd.ExecuteNonQuery();

            cmd.Dispose();
        }

    } 
    
    
    /// <summary>
    /// A class which represents the Fornecedor table in the BelDecor Database.
    /// </summary>
    public partial class Fornecedor: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<Fornecedor> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<Fornecedor>(new Domain.Model.BelDecorDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<Fornecedor> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(Fornecedor item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                Fornecedor item=new Fornecedor();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<Fornecedor> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Domain.Model.BelDecorDB _db;
        public Fornecedor(string connectionString, string providerName) {

            _db=new Domain.Model.BelDecorDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                Fornecedor.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<Fornecedor>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public Fornecedor(){
             _db=new Domain.Model.BelDecorDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public Fornecedor(Expression<Func<Fornecedor, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<Fornecedor> GetRepo(string connectionString, string providerName){
            Domain.Model.BelDecorDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Domain.Model.BelDecorDB();
            }else{
                db=new Domain.Model.BelDecorDB(connectionString, providerName);
            }
            IRepository<Fornecedor> _repo;
            
            if(db.TestMode){
                Fornecedor.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<Fornecedor>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<Fornecedor> GetRepo(){
            return GetRepo("","");
        }
        
        public static Fornecedor SingleOrDefault(Expression<Func<Fornecedor, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            Fornecedor single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static Fornecedor SingleOrDefault(Expression<Func<Fornecedor, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            Fornecedor single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<Fornecedor, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<Fornecedor, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<Fornecedor> Find(Expression<Func<Fornecedor, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<Fornecedor> Find(Expression<Func<Fornecedor, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<Fornecedor> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<Fornecedor> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<Fornecedor> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<Fornecedor> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<Fornecedor> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<Fornecedor> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "Id_fornecedor";
        }

        public object KeyValue()
        {
            return this.Id_fornecedor;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.nome.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(Fornecedor)){
                Fornecedor compare=(Fornecedor)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        //public override int GetHashCode() {
        //    return this.Id_fornecedor;
        //}
        
        public string DescriptorValue()
        {
                            return this.nome.ToString();
                    }

        public string DescriptorColumn() {
            return "nome";
        }
        public static string GetKeyColumn()
        {
            return "Id_fornecedor";
        }        
        public static string GetDescriptorColumn()
        {
            return "nome";
        }
        
        #region ' Foreign Keys '
        public IQueryable<Endereco> Enderecos
        {
            get
            {
                
                  var repo=Domain.Model.Endereco.GetRepo();
                  return from items in repo.GetAll()
                       where items.IdEndereco == _IdEndereco
                       select items;
            }
        }

        public IQueryable<ProdutoPedido> ProdutoPedidos
        {
            get
            {
                
                  var repo=Domain.Model.ProdutoPedido.GetRepo();
                  return from items in repo.GetAll()
                       where items.Id_Fornecedor == _Id_fornecedor
                       select items;
            }
        }

        public IQueryable<Produto> Produtos
        {
            get
            {
                
                  var repo=Domain.Model.Produto.GetRepo();
                  return from items in repo.GetAll()
                       where items.IdFornecedor == _Id_fornecedor
                       select items;
            }
        }

        public IQueryable<Telefone> Telefones
        {
            get
            {
                
                  var repo=Domain.Model.Telefone.GetRepo();
                  return from items in repo.GetAll()
                       where items.Id_Telefone == _IdTelefone
                       select items;
            }
        }

        #endregion
        

        int _Id_fornecedor;
        public int Id_fornecedor
        {
            get { return _Id_fornecedor; }
            set
            {
                if(_Id_fornecedor!=value){
                    _Id_fornecedor=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Id_fornecedor");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _nome;
        public string nome
        {
            get { return _nome; }
            set
            {
                if(_nome!=value){
                    _nome=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="nome");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _Razao_Social;
        public string Razao_Social
        {
            get { return _Razao_Social; }
            set
            {
                if(_Razao_Social!=value){
                    _Razao_Social=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Razao_Social");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _cnpj;
        public string cnpj
        {
            get { return _cnpj; }
            set
            {
                if(_cnpj!=value){
                    _cnpj=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="cnpj");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _cpf;
        public string cpf
        {
            get { return _cpf; }
            set
            {
                if(_cpf!=value){
                    _cpf=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="cpf");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _fax;
        public string fax
        {
            get { return _fax; }
            set
            {
                if(_fax!=value){
                    _fax=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="fax");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _email;
        public string email
        {
            get { return _email; }
            set
            {
                if(_email!=value){
                    _email=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="email");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        int? _IdEndereco;
        public int? IdEndereco
        {
            get { return _IdEndereco; }
            set
            {
                if(_IdEndereco!=value){
                    _IdEndereco=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="IdEndereco");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        int? _IdTelefone;
        public int? IdTelefone
        {
            get { return _IdTelefone; }
            set
            {
                if(_IdTelefone!=value){
                    _IdTelefone=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="IdTelefone");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _faxDDD;
        public string faxDDD
        {
            get { return _faxDDD; }
            set
            {
                if(_faxDDD!=value){
                    _faxDDD=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="faxDDD");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
		
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<Fornecedor, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }

		public void GeraAvaliacoes()
        {
            DbCommand cmd = _db.DataProvider.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_tempoAvaliacao";
            cmd.Connection = _db.DataProvider.CurrentSharedConnection;

            int r = cmd.ExecuteNonQuery();

            cmd.Dispose();
        }
        
		public void ReativaEmails()
        {
            DbCommand cmd = _db.DataProvider.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_reativaEmails";
            cmd.Connection = _db.DataProvider.CurrentSharedConnection;

            int r = cmd.ExecuteNonQuery();

            cmd.Dispose();
        }

    } 
    
    
    /// <summary>
    /// A class which represents the Funcionario table in the BelDecor Database.
    /// </summary>
    public partial class Funcionario: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<Funcionario> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<Funcionario>(new Domain.Model.BelDecorDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<Funcionario> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(Funcionario item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                Funcionario item=new Funcionario();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<Funcionario> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Domain.Model.BelDecorDB _db;
        public Funcionario(string connectionString, string providerName) {

            _db=new Domain.Model.BelDecorDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                Funcionario.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<Funcionario>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public Funcionario(){
             _db=new Domain.Model.BelDecorDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public Funcionario(Expression<Func<Funcionario, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<Funcionario> GetRepo(string connectionString, string providerName){
            Domain.Model.BelDecorDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Domain.Model.BelDecorDB();
            }else{
                db=new Domain.Model.BelDecorDB(connectionString, providerName);
            }
            IRepository<Funcionario> _repo;
            
            if(db.TestMode){
                Funcionario.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<Funcionario>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<Funcionario> GetRepo(){
            return GetRepo("","");
        }
        
        public static Funcionario SingleOrDefault(Expression<Func<Funcionario, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            Funcionario single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static Funcionario SingleOrDefault(Expression<Func<Funcionario, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            Funcionario single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<Funcionario, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<Funcionario, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<Funcionario> Find(Expression<Func<Funcionario, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<Funcionario> Find(Expression<Func<Funcionario, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<Funcionario> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<Funcionario> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<Funcionario> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<Funcionario> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<Funcionario> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<Funcionario> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "id";
        }

        public object KeyValue()
        {
            return this.id;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.Matricula.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(Funcionario)){
                Funcionario compare=(Funcionario)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        //public override int GetHashCode() {
        //    return this.id;
        //}
        
        public string DescriptorValue()
        {
                            return this.Matricula.ToString();
                    }

        public string DescriptorColumn() {
            return "Matricula";
        }
        public static string GetKeyColumn()
        {
            return "id";
        }        
        public static string GetDescriptorColumn()
        {
            return "Matricula";
        }
        
        #region ' Foreign Keys '
        public IQueryable<Pessoa> Pessoas
        {
            get
            {
                
                  var repo=Domain.Model.Pessoa.GetRepo();
                  return from items in repo.GetAll()
                       where items.idPessoa == _idPessoa
                       select items;
            }
        }

        #endregion
        

        int _id;
        public int id
        {
            get { return _id; }
            set
            {
                if(_id!=value){
                    _id=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _Matricula;
        public string Matricula
        {
            get { return _Matricula; }
            set
            {
                if(_Matricula!=value){
                    _Matricula=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Matricula");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        DateTime? _dataCadastro;
        public DateTime? dataCadastro
        {
            get { return _dataCadastro; }
            set
            {
                if(_dataCadastro!=value){
                    _dataCadastro=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="dataCadastro");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _Cargo;
        public string Cargo
        {
            get { return _Cargo; }
            set
            {
                if(_Cargo!=value){
                    _Cargo=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Cargo");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _Senha;
        public string Senha
        {
            get { return _Senha; }
            set
            {
                if(_Senha!=value){
                    _Senha=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Senha");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _login;
        public string login
        {
            get { return _login; }
            set
            {
                if(_login!=value){
                    _login=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="login");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        int? _idPessoa;
        public int? idPessoa
        {
            get { return _idPessoa; }
            set
            {
                if(_idPessoa!=value){
                    _idPessoa=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="idPessoa");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        int? _Perfil;
        public int? Perfil
        {
            get { return _Perfil; }
            set
            {
                if(_Perfil!=value){
                    _Perfil=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Perfil");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
		
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<Funcionario, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }

		public void GeraAvaliacoes()
        {
            DbCommand cmd = _db.DataProvider.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_tempoAvaliacao";
            cmd.Connection = _db.DataProvider.CurrentSharedConnection;

            int r = cmd.ExecuteNonQuery();

            cmd.Dispose();
        }
        
		public void ReativaEmails()
        {
            DbCommand cmd = _db.DataProvider.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_reativaEmails";
            cmd.Connection = _db.DataProvider.CurrentSharedConnection;

            int r = cmd.ExecuteNonQuery();

            cmd.Dispose();
        }

    } 
    
    
    /// <summary>
    /// A class which represents the Numerador table in the BelDecor Database.
    /// </summary>
    public partial class Numerador: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<Numerador> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<Numerador>(new Domain.Model.BelDecorDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<Numerador> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(Numerador item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                Numerador item=new Numerador();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<Numerador> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Domain.Model.BelDecorDB _db;
        public Numerador(string connectionString, string providerName) {

            _db=new Domain.Model.BelDecorDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                Numerador.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<Numerador>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public Numerador(){
             _db=new Domain.Model.BelDecorDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public Numerador(Expression<Func<Numerador, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<Numerador> GetRepo(string connectionString, string providerName){
            Domain.Model.BelDecorDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Domain.Model.BelDecorDB();
            }else{
                db=new Domain.Model.BelDecorDB(connectionString, providerName);
            }
            IRepository<Numerador> _repo;
            
            if(db.TestMode){
                Numerador.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<Numerador>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<Numerador> GetRepo(){
            return GetRepo("","");
        }
        
        public static Numerador SingleOrDefault(Expression<Func<Numerador, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            Numerador single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static Numerador SingleOrDefault(Expression<Func<Numerador, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            Numerador single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<Numerador, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<Numerador, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<Numerador> Find(Expression<Func<Numerador, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<Numerador> Find(Expression<Func<Numerador, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<Numerador> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<Numerador> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<Numerador> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<Numerador> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<Numerador> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<Numerador> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "idNumerador";
        }

        public object KeyValue()
        {
            return this.idNumerador;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.NomeNumerador.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(Numerador)){
                Numerador compare=(Numerador)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        //public override int GetHashCode() {
        //    return this.idNumerador;
        //}
        
        public string DescriptorValue()
        {
                            return this.NomeNumerador.ToString();
                    }

        public string DescriptorColumn() {
            return "NomeNumerador";
        }
        public static string GetKeyColumn()
        {
            return "idNumerador";
        }        
        public static string GetDescriptorColumn()
        {
            return "NomeNumerador";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _idNumerador;
        public int idNumerador
        {
            get { return _idNumerador; }
            set
            {
                if(_idNumerador!=value){
                    _idNumerador=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="idNumerador");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _NomeNumerador;
        public string NomeNumerador
        {
            get { return _NomeNumerador; }
            set
            {
                if(_NomeNumerador!=value){
                    _NomeNumerador=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="NomeNumerador");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        long? _ValorNumerador;
        public long? ValorNumerador
        {
            get { return _ValorNumerador; }
            set
            {
                if(_ValorNumerador!=value){
                    _ValorNumerador=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ValorNumerador");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
		
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<Numerador, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }

		public void GeraAvaliacoes()
        {
            DbCommand cmd = _db.DataProvider.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_tempoAvaliacao";
            cmd.Connection = _db.DataProvider.CurrentSharedConnection;

            int r = cmd.ExecuteNonQuery();

            cmd.Dispose();
        }
        
		public void ReativaEmails()
        {
            DbCommand cmd = _db.DataProvider.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_reativaEmails";
            cmd.Connection = _db.DataProvider.CurrentSharedConnection;

            int r = cmd.ExecuteNonQuery();

            cmd.Dispose();
        }

    } 
    
    
    /// <summary>
    /// A class which represents the Pedido table in the BelDecor Database.
    /// </summary>
    public partial class Pedido: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<Pedido> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<Pedido>(new Domain.Model.BelDecorDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<Pedido> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(Pedido item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                Pedido item=new Pedido();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<Pedido> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Domain.Model.BelDecorDB _db;
        public Pedido(string connectionString, string providerName) {

            _db=new Domain.Model.BelDecorDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                Pedido.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<Pedido>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public Pedido(){
             _db=new Domain.Model.BelDecorDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public Pedido(Expression<Func<Pedido, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<Pedido> GetRepo(string connectionString, string providerName){
            Domain.Model.BelDecorDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Domain.Model.BelDecorDB();
            }else{
                db=new Domain.Model.BelDecorDB(connectionString, providerName);
            }
            IRepository<Pedido> _repo;
            
            if(db.TestMode){
                Pedido.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<Pedido>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<Pedido> GetRepo(){
            return GetRepo("","");
        }
        
        public static Pedido SingleOrDefault(Expression<Func<Pedido, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            Pedido single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static Pedido SingleOrDefault(Expression<Func<Pedido, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            Pedido single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<Pedido, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<Pedido, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<Pedido> Find(Expression<Func<Pedido, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<Pedido> Find(Expression<Func<Pedido, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<Pedido> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<Pedido> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<Pedido> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<Pedido> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<Pedido> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<Pedido> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "Id_Pedido";
        }

        public object KeyValue()
        {
            return this.Id_Pedido;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.observacoes.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(Pedido)){
                Pedido compare=(Pedido)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        //public override int GetHashCode() {
        //    return this.Id_Pedido;
        //}
        
        public string DescriptorValue()
        {
                            return this.observacoes.ToString();
                    }

        public string DescriptorColumn() {
            return "observacoes";
        }
        public static string GetKeyColumn()
        {
            return "Id_Pedido";
        }        
        public static string GetDescriptorColumn()
        {
            return "observacoes";
        }
        
        #region ' Foreign Keys '
        public IQueryable<Cliente> Clientes
        {
            get
            {
                
                  var repo=Domain.Model.Cliente.GetRepo();
                  return from items in repo.GetAll()
                       where items.id == _IdCliente
                       select items;
            }
        }

        public IQueryable<ContaReceber> ContaRecebers
        {
            get
            {
                
                  var repo=Domain.Model.ContaReceber.GetRepo();
                  return from items in repo.GetAll()
                       where items.Id_Pedido == _Id_Pedido
                       select items;
            }
        }

        public IQueryable<ProdutoPedido> ProdutoPedidos
        {
            get
            {
                
                  var repo=Domain.Model.ProdutoPedido.GetRepo();
                  return from items in repo.GetAll()
                       where items.Id_Pedido == _Id_Pedido
                       select items;
            }
        }

        public IQueryable<ServicoPedido> ServicoPedidos
        {
            get
            {
                
                  var repo=Domain.Model.ServicoPedido.GetRepo();
                  return from items in repo.GetAll()
                       where items.Id_Pedido == _Id_Pedido
                       select items;
            }
        }

        public IQueryable<StatusPedido> StatusPedidos
        {
            get
            {
                
                  var repo=Domain.Model.StatusPedido.GetRepo();
                  return from items in repo.GetAll()
                       where items.Id_Pedido == _Id_Pedido
                       select items;
            }
        }

        #endregion
        

        int _Id_Pedido;
        public int Id_Pedido
        {
            get { return _Id_Pedido; }
            set
            {
                if(_Id_Pedido!=value){
                    _Id_Pedido=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Id_Pedido");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _observacoes;
        public string observacoes
        {
            get { return _observacoes; }
            set
            {
                if(_observacoes!=value){
                    _observacoes=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="observacoes");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        int? _IdCliente;
        public int? IdCliente
        {
            get { return _IdCliente; }
            set
            {
                if(_IdCliente!=value){
                    _IdCliente=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="IdCliente");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        DateTime? _dataCadastro;
        public DateTime? dataCadastro
        {
            get { return _dataCadastro; }
            set
            {
                if(_dataCadastro!=value){
                    _dataCadastro=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="dataCadastro");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        DateTime? _dataVencimento;
        public DateTime? dataVencimento
        {
            get { return _dataVencimento; }
            set
            {
                if(_dataVencimento!=value){
                    _dataVencimento=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="dataVencimento");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        decimal? _ValorTotal;
        public decimal? ValorTotal
        {
            get { return _ValorTotal; }
            set
            {
                if(_ValorTotal!=value){
                    _ValorTotal=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ValorTotal");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _CodigoPedido;
        public string CodigoPedido
        {
            get { return _CodigoPedido; }
            set
            {
                if(_CodigoPedido!=value){
                    _CodigoPedido=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="CodigoPedido");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        bool? _PagamentoEfetuado;
        public bool? PagamentoEfetuado
        {
            get { return _PagamentoEfetuado; }
            set
            {
                if(_PagamentoEfetuado!=value){
                    _PagamentoEfetuado=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="PagamentoEfetuado");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
		
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<Pedido, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }

		public void GeraAvaliacoes()
        {
            DbCommand cmd = _db.DataProvider.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_tempoAvaliacao";
            cmd.Connection = _db.DataProvider.CurrentSharedConnection;

            int r = cmd.ExecuteNonQuery();

            cmd.Dispose();
        }
        
		public void ReativaEmails()
        {
            DbCommand cmd = _db.DataProvider.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_reativaEmails";
            cmd.Connection = _db.DataProvider.CurrentSharedConnection;

            int r = cmd.ExecuteNonQuery();

            cmd.Dispose();
        }

    } 
    
    
    /// <summary>
    /// A class which represents the PerfilPagina table in the BelDecor Database.
    /// </summary>
    public partial class PerfilPagina: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<PerfilPagina> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<PerfilPagina>(new Domain.Model.BelDecorDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<PerfilPagina> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(PerfilPagina item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                PerfilPagina item=new PerfilPagina();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<PerfilPagina> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Domain.Model.BelDecorDB _db;
        public PerfilPagina(string connectionString, string providerName) {

            _db=new Domain.Model.BelDecorDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                PerfilPagina.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<PerfilPagina>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public PerfilPagina(){
             _db=new Domain.Model.BelDecorDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public PerfilPagina(Expression<Func<PerfilPagina, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<PerfilPagina> GetRepo(string connectionString, string providerName){
            Domain.Model.BelDecorDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Domain.Model.BelDecorDB();
            }else{
                db=new Domain.Model.BelDecorDB(connectionString, providerName);
            }
            IRepository<PerfilPagina> _repo;
            
            if(db.TestMode){
                PerfilPagina.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<PerfilPagina>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<PerfilPagina> GetRepo(){
            return GetRepo("","");
        }
        
        public static PerfilPagina SingleOrDefault(Expression<Func<PerfilPagina, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            PerfilPagina single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static PerfilPagina SingleOrDefault(Expression<Func<PerfilPagina, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            PerfilPagina single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<PerfilPagina, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<PerfilPagina, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<PerfilPagina> Find(Expression<Func<PerfilPagina, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<PerfilPagina> Find(Expression<Func<PerfilPagina, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<PerfilPagina> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<PerfilPagina> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<PerfilPagina> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<PerfilPagina> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<PerfilPagina> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<PerfilPagina> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "id";
        }

        public object KeyValue()
        {
            return this.id;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.NomePagina.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(PerfilPagina)){
                PerfilPagina compare=(PerfilPagina)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        //public override int GetHashCode() {
        //    return this.id;
        //}
        
        public string DescriptorValue()
        {
                            return this.NomePagina.ToString();
                    }

        public string DescriptorColumn() {
            return "NomePagina";
        }
        public static string GetKeyColumn()
        {
            return "id";
        }        
        public static string GetDescriptorColumn()
        {
            return "NomePagina";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _id;
        public int id
        {
            get { return _id; }
            set
            {
                if(_id!=value){
                    _id=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        int? _IdPerfil;
        public int? IdPerfil
        {
            get { return _IdPerfil; }
            set
            {
                if(_IdPerfil!=value){
                    _IdPerfil=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="IdPerfil");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _NomePagina;
        public string NomePagina
        {
            get { return _NomePagina; }
            set
            {
                if(_NomePagina!=value){
                    _NomePagina=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="NomePagina");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
		
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<PerfilPagina, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }

		public void GeraAvaliacoes()
        {
            DbCommand cmd = _db.DataProvider.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_tempoAvaliacao";
            cmd.Connection = _db.DataProvider.CurrentSharedConnection;

            int r = cmd.ExecuteNonQuery();

            cmd.Dispose();
        }
        
		public void ReativaEmails()
        {
            DbCommand cmd = _db.DataProvider.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_reativaEmails";
            cmd.Connection = _db.DataProvider.CurrentSharedConnection;

            int r = cmd.ExecuteNonQuery();

            cmd.Dispose();
        }

    } 
    
    
    /// <summary>
    /// A class which represents the Pessoa table in the BelDecor Database.
    /// </summary>
    public partial class Pessoa: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<Pessoa> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<Pessoa>(new Domain.Model.BelDecorDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<Pessoa> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(Pessoa item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                Pessoa item=new Pessoa();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<Pessoa> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Domain.Model.BelDecorDB _db;
        public Pessoa(string connectionString, string providerName) {

            _db=new Domain.Model.BelDecorDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                Pessoa.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<Pessoa>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public Pessoa(){
             _db=new Domain.Model.BelDecorDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public Pessoa(Expression<Func<Pessoa, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<Pessoa> GetRepo(string connectionString, string providerName){
            Domain.Model.BelDecorDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Domain.Model.BelDecorDB();
            }else{
                db=new Domain.Model.BelDecorDB(connectionString, providerName);
            }
            IRepository<Pessoa> _repo;
            
            if(db.TestMode){
                Pessoa.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<Pessoa>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<Pessoa> GetRepo(){
            return GetRepo("","");
        }
        
        public static Pessoa SingleOrDefault(Expression<Func<Pessoa, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            Pessoa single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static Pessoa SingleOrDefault(Expression<Func<Pessoa, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            Pessoa single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<Pessoa, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<Pessoa, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<Pessoa> Find(Expression<Func<Pessoa, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<Pessoa> Find(Expression<Func<Pessoa, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<Pessoa> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<Pessoa> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<Pessoa> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<Pessoa> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<Pessoa> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<Pessoa> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "idPessoa";
        }

        public object KeyValue()
        {
            return this.idPessoa;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.nome.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(Pessoa)){
                Pessoa compare=(Pessoa)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        //public override int GetHashCode() {
        //    return this.idPessoa;
        //}
        
        public string DescriptorValue()
        {
                            return this.nome.ToString();
                    }

        public string DescriptorColumn() {
            return "nome";
        }
        public static string GetKeyColumn()
        {
            return "idPessoa";
        }        
        public static string GetDescriptorColumn()
        {
            return "nome";
        }
        
        #region ' Foreign Keys '
        public IQueryable<Endereco> Enderecos
        {
            get
            {
                
                  var repo=Domain.Model.Endereco.GetRepo();
                  return from items in repo.GetAll()
                       where items.IdEndereco == _idEndereco
                       select items;
            }
        }

        public IQueryable<Cliente> Clientes
        {
            get
            {
                
                  var repo=Domain.Model.Cliente.GetRepo();
                  return from items in repo.GetAll()
                       where items.IdPessoa == _idPessoa
                       select items;
            }
        }

        public IQueryable<Funcionario> Funcionarios
        {
            get
            {
                
                  var repo=Domain.Model.Funcionario.GetRepo();
                  return from items in repo.GetAll()
                       where items.idPessoa == _idPessoa
                       select items;
            }
        }

        public IQueryable<Telefone> Telefones
        {
            get
            {
                
                  var repo=Domain.Model.Telefone.GetRepo();
                  return from items in repo.GetAll()
                       where items.Id_Telefone == _IdTelefone
                       select items;
            }
        }

        #endregion
        

        string _nome;
        public string nome
        {
            get { return _nome; }
            set
            {
                if(_nome!=value){
                    _nome=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="nome");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _email;
        public string email
        {
            get { return _email; }
            set
            {
                if(_email!=value){
                    _email=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="email");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _sexo;
        public string sexo
        {
            get { return _sexo; }
            set
            {
                if(_sexo!=value){
                    _sexo=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="sexo");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        DateTime? _dataNascimento;
        public DateTime? dataNascimento
        {
            get { return _dataNascimento; }
            set
            {
                if(_dataNascimento!=value){
                    _dataNascimento=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="dataNascimento");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _cpf;
        public string cpf
        {
            get { return _cpf; }
            set
            {
                if(_cpf!=value){
                    _cpf=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="cpf");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _rg;
        public string rg
        {
            get { return _rg; }
            set
            {
                if(_rg!=value){
                    _rg=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="rg");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        DateTime? _dataCadastro;
        public DateTime? dataCadastro
        {
            get { return _dataCadastro; }
            set
            {
                if(_dataCadastro!=value){
                    _dataCadastro=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="dataCadastro");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        int _idPessoa;
        public int idPessoa
        {
            get { return _idPessoa; }
            set
            {
                if(_idPessoa!=value){
                    _idPessoa=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="idPessoa");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        int? _idEndereco;
        public int? idEndereco
        {
            get { return _idEndereco; }
            set
            {
                if(_idEndereco!=value){
                    _idEndereco=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="idEndereco");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _cnpj;
        public string cnpj
        {
            get { return _cnpj; }
            set
            {
                if(_cnpj!=value){
                    _cnpj=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="cnpj");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        int? _IdTelefone;
        public int? IdTelefone
        {
            get { return _IdTelefone; }
            set
            {
                if(_IdTelefone!=value){
                    _IdTelefone=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="IdTelefone");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
		
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<Pessoa, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }

		public void GeraAvaliacoes()
        {
            DbCommand cmd = _db.DataProvider.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_tempoAvaliacao";
            cmd.Connection = _db.DataProvider.CurrentSharedConnection;

            int r = cmd.ExecuteNonQuery();

            cmd.Dispose();
        }
        
		public void ReativaEmails()
        {
            DbCommand cmd = _db.DataProvider.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_reativaEmails";
            cmd.Connection = _db.DataProvider.CurrentSharedConnection;

            int r = cmd.ExecuteNonQuery();

            cmd.Dispose();
        }

    } 
    
    
    /// <summary>
    /// A class which represents the Produto table in the BelDecor Database.
    /// </summary>
    public partial class Produto: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<Produto> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<Produto>(new Domain.Model.BelDecorDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<Produto> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(Produto item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                Produto item=new Produto();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<Produto> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Domain.Model.BelDecorDB _db;
        public Produto(string connectionString, string providerName) {

            _db=new Domain.Model.BelDecorDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                Produto.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<Produto>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public Produto(){
             _db=new Domain.Model.BelDecorDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public Produto(Expression<Func<Produto, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<Produto> GetRepo(string connectionString, string providerName){
            Domain.Model.BelDecorDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Domain.Model.BelDecorDB();
            }else{
                db=new Domain.Model.BelDecorDB(connectionString, providerName);
            }
            IRepository<Produto> _repo;
            
            if(db.TestMode){
                Produto.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<Produto>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<Produto> GetRepo(){
            return GetRepo("","");
        }
        
        public static Produto SingleOrDefault(Expression<Func<Produto, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            Produto single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static Produto SingleOrDefault(Expression<Func<Produto, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            Produto single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<Produto, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<Produto, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<Produto> Find(Expression<Func<Produto, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<Produto> Find(Expression<Func<Produto, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<Produto> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<Produto> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<Produto> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<Produto> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<Produto> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<Produto> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "Id_Produto";
        }

        public object KeyValue()
        {
            return this.Id_Produto;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.nome.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(Produto)){
                Produto compare=(Produto)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        //public override int GetHashCode() {
        //    return this.Id_Produto;
        //}
        
        public string DescriptorValue()
        {
                            return this.nome.ToString();
                    }

        public string DescriptorColumn() {
            return "nome";
        }
        public static string GetKeyColumn()
        {
            return "Id_Produto";
        }        
        public static string GetDescriptorColumn()
        {
            return "nome";
        }
        
        #region ' Foreign Keys '
        public IQueryable<Fornecedor> Fornecedors
        {
            get
            {
                
                  var repo=Domain.Model.Fornecedor.GetRepo();
                  return from items in repo.GetAll()
                       where items.Id_fornecedor == _IdFornecedor
                       select items;
            }
        }

        public IQueryable<ProdutoPedido> ProdutoPedidos
        {
            get
            {
                
                  var repo=Domain.Model.ProdutoPedido.GetRepo();
                  return from items in repo.GetAll()
                       where items.Id_Produto == _Id_Produto
                       select items;
            }
        }

        #endregion
        

        int _Id_Produto;
        public int Id_Produto
        {
            get { return _Id_Produto; }
            set
            {
                if(_Id_Produto!=value){
                    _Id_Produto=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Id_Produto");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _nome;
        public string nome
        {
            get { return _nome; }
            set
            {
                if(_nome!=value){
                    _nome=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="nome");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _observacoes;
        public string observacoes
        {
            get { return _observacoes; }
            set
            {
                if(_observacoes!=value){
                    _observacoes=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="observacoes");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _codigoProduto;
        public string codigoProduto
        {
            get { return _codigoProduto; }
            set
            {
                if(_codigoProduto!=value){
                    _codigoProduto=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="codigoProduto");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        int? _IdFornecedor;
        public int? IdFornecedor
        {
            get { return _IdFornecedor; }
            set
            {
                if(_IdFornecedor!=value){
                    _IdFornecedor=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="IdFornecedor");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
		
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<Produto, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }

		public void GeraAvaliacoes()
        {
            DbCommand cmd = _db.DataProvider.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_tempoAvaliacao";
            cmd.Connection = _db.DataProvider.CurrentSharedConnection;

            int r = cmd.ExecuteNonQuery();

            cmd.Dispose();
        }
        
		public void ReativaEmails()
        {
            DbCommand cmd = _db.DataProvider.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_reativaEmails";
            cmd.Connection = _db.DataProvider.CurrentSharedConnection;

            int r = cmd.ExecuteNonQuery();

            cmd.Dispose();
        }

    } 
    
    
    /// <summary>
    /// A class which represents the ProdutoPedido table in the BelDecor Database.
    /// </summary>
    public partial class ProdutoPedido: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<ProdutoPedido> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<ProdutoPedido>(new Domain.Model.BelDecorDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<ProdutoPedido> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(ProdutoPedido item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                ProdutoPedido item=new ProdutoPedido();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<ProdutoPedido> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Domain.Model.BelDecorDB _db;
        public ProdutoPedido(string connectionString, string providerName) {

            _db=new Domain.Model.BelDecorDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                ProdutoPedido.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ProdutoPedido>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public ProdutoPedido(){
             _db=new Domain.Model.BelDecorDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public ProdutoPedido(Expression<Func<ProdutoPedido, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<ProdutoPedido> GetRepo(string connectionString, string providerName){
            Domain.Model.BelDecorDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Domain.Model.BelDecorDB();
            }else{
                db=new Domain.Model.BelDecorDB(connectionString, providerName);
            }
            IRepository<ProdutoPedido> _repo;
            
            if(db.TestMode){
                ProdutoPedido.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ProdutoPedido>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<ProdutoPedido> GetRepo(){
            return GetRepo("","");
        }
        
        public static ProdutoPedido SingleOrDefault(Expression<Func<ProdutoPedido, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            ProdutoPedido single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static ProdutoPedido SingleOrDefault(Expression<Func<ProdutoPedido, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            ProdutoPedido single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<ProdutoPedido, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<ProdutoPedido, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<ProdutoPedido> Find(Expression<Func<ProdutoPedido, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<ProdutoPedido> Find(Expression<Func<ProdutoPedido, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<ProdutoPedido> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<ProdutoPedido> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<ProdutoPedido> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<ProdutoPedido> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<ProdutoPedido> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<ProdutoPedido> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "Id_ProdutoPedido";
        }

        public object KeyValue()
        {
            return this.Id_ProdutoPedido;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.cor.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(ProdutoPedido)){
                ProdutoPedido compare=(ProdutoPedido)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        //public override int GetHashCode() {
        //    return this.Id_ProdutoPedido;
        //}
        
        public string DescriptorValue()
        {
                            return this.cor.ToString();
                    }

        public string DescriptorColumn() {
            return "cor";
        }
        public static string GetKeyColumn()
        {
            return "Id_ProdutoPedido";
        }        
        public static string GetDescriptorColumn()
        {
            return "cor";
        }
        
        #region ' Foreign Keys '
        public IQueryable<Fornecedor> Fornecedors
        {
            get
            {
                
                  var repo=Domain.Model.Fornecedor.GetRepo();
                  return from items in repo.GetAll()
                       where items.Id_fornecedor == _Id_Fornecedor
                       select items;
            }
        }

        public IQueryable<Pedido> Pedidos
        {
            get
            {
                
                  var repo=Domain.Model.Pedido.GetRepo();
                  return from items in repo.GetAll()
                       where items.Id_Pedido == _Id_Pedido
                       select items;
            }
        }

        public IQueryable<Produto> Produtos
        {
            get
            {
                
                  var repo=Domain.Model.Produto.GetRepo();
                  return from items in repo.GetAll()
                       where items.Id_Produto == _Id_Produto
                       select items;
            }
        }

        #endregion
        

        int _Id_ProdutoPedido;
        public int Id_ProdutoPedido
        {
            get { return _Id_ProdutoPedido; }
            set
            {
                if(_Id_ProdutoPedido!=value){
                    _Id_ProdutoPedido=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Id_ProdutoPedido");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        int _Id_Produto;
        public int Id_Produto
        {
            get { return _Id_Produto; }
            set
            {
                if(_Id_Produto!=value){
                    _Id_Produto=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Id_Produto");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        int _Id_Pedido;
        public int Id_Pedido
        {
            get { return _Id_Pedido; }
            set
            {
                if(_Id_Pedido!=value){
                    _Id_Pedido=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Id_Pedido");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        int? _Id_Fornecedor;
        public int? Id_Fornecedor
        {
            get { return _Id_Fornecedor; }
            set
            {
                if(_Id_Fornecedor!=value){
                    _Id_Fornecedor=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Id_Fornecedor");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        decimal? _valor;
        public decimal? valor
        {
            get { return _valor; }
            set
            {
                if(_valor!=value){
                    _valor=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="valor");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        decimal? _desconto;
        public decimal? desconto
        {
            get { return _desconto; }
            set
            {
                if(_desconto!=value){
                    _desconto=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="desconto");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        int? _quantidade;
        public int? quantidade
        {
            get { return _quantidade; }
            set
            {
                if(_quantidade!=value){
                    _quantidade=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="quantidade");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        double? _altura;
        public double? altura
        {
            get { return _altura; }
            set
            {
                if(_altura!=value){
                    _altura=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="altura");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        double? _largura;
        public double? largura
        {
            get { return _largura; }
            set
            {
                if(_largura!=value){
                    _largura=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="largura");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        double? _espessura;
        public double? espessura
        {
            get { return _espessura; }
            set
            {
                if(_espessura!=value){
                    _espessura=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="espessura");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        double? _profundidade;
        public double? profundidade
        {
            get { return _profundidade; }
            set
            {
                if(_profundidade!=value){
                    _profundidade=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="profundidade");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _cor;
        public string cor
        {
            get { return _cor; }
            set
            {
                if(_cor!=value){
                    _cor=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="cor");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
		
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<ProdutoPedido, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }

		public void GeraAvaliacoes()
        {
            DbCommand cmd = _db.DataProvider.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_tempoAvaliacao";
            cmd.Connection = _db.DataProvider.CurrentSharedConnection;

            int r = cmd.ExecuteNonQuery();

            cmd.Dispose();
        }
        
		public void ReativaEmails()
        {
            DbCommand cmd = _db.DataProvider.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_reativaEmails";
            cmd.Connection = _db.DataProvider.CurrentSharedConnection;

            int r = cmd.ExecuteNonQuery();

            cmd.Dispose();
        }

    } 
    
    
    /// <summary>
    /// A class which represents the Servico table in the BelDecor Database.
    /// </summary>
    public partial class Servico: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<Servico> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<Servico>(new Domain.Model.BelDecorDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<Servico> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(Servico item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                Servico item=new Servico();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<Servico> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Domain.Model.BelDecorDB _db;
        public Servico(string connectionString, string providerName) {

            _db=new Domain.Model.BelDecorDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                Servico.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<Servico>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public Servico(){
             _db=new Domain.Model.BelDecorDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public Servico(Expression<Func<Servico, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<Servico> GetRepo(string connectionString, string providerName){
            Domain.Model.BelDecorDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Domain.Model.BelDecorDB();
            }else{
                db=new Domain.Model.BelDecorDB(connectionString, providerName);
            }
            IRepository<Servico> _repo;
            
            if(db.TestMode){
                Servico.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<Servico>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<Servico> GetRepo(){
            return GetRepo("","");
        }
        
        public static Servico SingleOrDefault(Expression<Func<Servico, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            Servico single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static Servico SingleOrDefault(Expression<Func<Servico, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            Servico single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<Servico, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<Servico, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<Servico> Find(Expression<Func<Servico, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<Servico> Find(Expression<Func<Servico, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<Servico> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<Servico> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<Servico> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<Servico> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<Servico> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<Servico> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "Id_Servico";
        }

        public object KeyValue()
        {
            return this.Id_Servico;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.tipo.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(Servico)){
                Servico compare=(Servico)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        //public override int GetHashCode() {
        //    return this.Id_Servico;
        //}
        
        public string DescriptorValue()
        {
                            return this.tipo.ToString();
                    }

        public string DescriptorColumn() {
            return "tipo";
        }
        public static string GetKeyColumn()
        {
            return "Id_Servico";
        }        
        public static string GetDescriptorColumn()
        {
            return "tipo";
        }
        
        #region ' Foreign Keys '
        public IQueryable<ServicoPedido> ServicoPedidos
        {
            get
            {
                
                  var repo=Domain.Model.ServicoPedido.GetRepo();
                  return from items in repo.GetAll()
                       where items.Id_Servico == _Id_Servico
                       select items;
            }
        }

        #endregion
        

        int _Id_Servico;
        public int Id_Servico
        {
            get { return _Id_Servico; }
            set
            {
                if(_Id_Servico!=value){
                    _Id_Servico=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Id_Servico");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _tipo;
        public string tipo
        {
            get { return _tipo; }
            set
            {
                if(_tipo!=value){
                    _tipo=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="tipo");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _descricao;
        public string descricao
        {
            get { return _descricao; }
            set
            {
                if(_descricao!=value){
                    _descricao=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="descricao");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        decimal? _Preco;
        public decimal? Preco
        {
            get { return _Preco; }
            set
            {
                if(_Preco!=value){
                    _Preco=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Preco");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
		
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<Servico, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }

		public void GeraAvaliacoes()
        {
            DbCommand cmd = _db.DataProvider.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_tempoAvaliacao";
            cmd.Connection = _db.DataProvider.CurrentSharedConnection;

            int r = cmd.ExecuteNonQuery();

            cmd.Dispose();
        }
        
		public void ReativaEmails()
        {
            DbCommand cmd = _db.DataProvider.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_reativaEmails";
            cmd.Connection = _db.DataProvider.CurrentSharedConnection;

            int r = cmd.ExecuteNonQuery();

            cmd.Dispose();
        }

    } 
    
    
    /// <summary>
    /// A class which represents the ServicoPedido table in the BelDecor Database.
    /// </summary>
    public partial class ServicoPedido: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<ServicoPedido> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<ServicoPedido>(new Domain.Model.BelDecorDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<ServicoPedido> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(ServicoPedido item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                ServicoPedido item=new ServicoPedido();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<ServicoPedido> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Domain.Model.BelDecorDB _db;
        public ServicoPedido(string connectionString, string providerName) {

            _db=new Domain.Model.BelDecorDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                ServicoPedido.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ServicoPedido>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public ServicoPedido(){
             _db=new Domain.Model.BelDecorDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public ServicoPedido(Expression<Func<ServicoPedido, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<ServicoPedido> GetRepo(string connectionString, string providerName){
            Domain.Model.BelDecorDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Domain.Model.BelDecorDB();
            }else{
                db=new Domain.Model.BelDecorDB(connectionString, providerName);
            }
            IRepository<ServicoPedido> _repo;
            
            if(db.TestMode){
                ServicoPedido.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ServicoPedido>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<ServicoPedido> GetRepo(){
            return GetRepo("","");
        }
        
        public static ServicoPedido SingleOrDefault(Expression<Func<ServicoPedido, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            ServicoPedido single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static ServicoPedido SingleOrDefault(Expression<Func<ServicoPedido, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            ServicoPedido single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<ServicoPedido, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<ServicoPedido, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<ServicoPedido> Find(Expression<Func<ServicoPedido, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<ServicoPedido> Find(Expression<Func<ServicoPedido, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<ServicoPedido> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<ServicoPedido> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<ServicoPedido> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<ServicoPedido> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<ServicoPedido> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<ServicoPedido> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "Id_SevicoPedido";
        }

        public object KeyValue()
        {
            return this.Id_SevicoPedido;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.Id_Servico.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(ServicoPedido)){
                ServicoPedido compare=(ServicoPedido)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        //public override int GetHashCode() {
        //    return this.Id_SevicoPedido;
        //}
        
        public string DescriptorValue()
        {
                            return this.Id_Servico.ToString();
                    }

        public string DescriptorColumn() {
            return "Id_Servico";
        }
        public static string GetKeyColumn()
        {
            return "Id_SevicoPedido";
        }        
        public static string GetDescriptorColumn()
        {
            return "Id_Servico";
        }
        
        #region ' Foreign Keys '
        public IQueryable<Pedido> Pedidos
        {
            get
            {
                
                  var repo=Domain.Model.Pedido.GetRepo();
                  return from items in repo.GetAll()
                       where items.Id_Pedido == _Id_Pedido
                       select items;
            }
        }

        public IQueryable<Servico> Servicos
        {
            get
            {
                
                  var repo=Domain.Model.Servico.GetRepo();
                  return from items in repo.GetAll()
                       where items.Id_Servico == _Id_Servico
                       select items;
            }
        }

        #endregion
        

        int _Id_SevicoPedido;
        public int Id_SevicoPedido
        {
            get { return _Id_SevicoPedido; }
            set
            {
                if(_Id_SevicoPedido!=value){
                    _Id_SevicoPedido=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Id_SevicoPedido");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        int _Id_Servico;
        public int Id_Servico
        {
            get { return _Id_Servico; }
            set
            {
                if(_Id_Servico!=value){
                    _Id_Servico=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Id_Servico");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        int _Id_Pedido;
        public int Id_Pedido
        {
            get { return _Id_Pedido; }
            set
            {
                if(_Id_Pedido!=value){
                    _Id_Pedido=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Id_Pedido");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        decimal? _Desconto;
        public decimal? Desconto
        {
            get { return _Desconto; }
            set
            {
                if(_Desconto!=value){
                    _Desconto=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Desconto");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        decimal? _valor;
        public decimal? valor
        {
            get { return _valor; }
            set
            {
                if(_valor!=value){
                    _valor=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="valor");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
		
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<ServicoPedido, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }

		public void GeraAvaliacoes()
        {
            DbCommand cmd = _db.DataProvider.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_tempoAvaliacao";
            cmd.Connection = _db.DataProvider.CurrentSharedConnection;

            int r = cmd.ExecuteNonQuery();

            cmd.Dispose();
        }
        
		public void ReativaEmails()
        {
            DbCommand cmd = _db.DataProvider.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_reativaEmails";
            cmd.Connection = _db.DataProvider.CurrentSharedConnection;

            int r = cmd.ExecuteNonQuery();

            cmd.Dispose();
        }

    } 
    
    
    /// <summary>
    /// A class which represents the Status table in the BelDecor Database.
    /// </summary>
    public partial class Status: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<Status> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<Status>(new Domain.Model.BelDecorDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<Status> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(Status item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                Status item=new Status();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<Status> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Domain.Model.BelDecorDB _db;
        public Status(string connectionString, string providerName) {

            _db=new Domain.Model.BelDecorDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                Status.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<Status>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public Status(){
             _db=new Domain.Model.BelDecorDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public Status(Expression<Func<Status, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<Status> GetRepo(string connectionString, string providerName){
            Domain.Model.BelDecorDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Domain.Model.BelDecorDB();
            }else{
                db=new Domain.Model.BelDecorDB(connectionString, providerName);
            }
            IRepository<Status> _repo;
            
            if(db.TestMode){
                Status.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<Status>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<Status> GetRepo(){
            return GetRepo("","");
        }
        
        public static Status SingleOrDefault(Expression<Func<Status, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            Status single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static Status SingleOrDefault(Expression<Func<Status, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            Status single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<Status, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<Status, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<Status> Find(Expression<Func<Status, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<Status> Find(Expression<Func<Status, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<Status> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<Status> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<Status> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<Status> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<Status> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<Status> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "Id_Status";
        }

        public object KeyValue()
        {
            return this.Id_Status;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.P_status.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(Status)){
                Status compare=(Status)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        //public override int GetHashCode() {
        //    return this.Id_Status;
        //}
        
        public string DescriptorValue()
        {
                            return this.P_status.ToString();
                    }

        public string DescriptorColumn() {
            return "P_status";
        }
        public static string GetKeyColumn()
        {
            return "Id_Status";
        }        
        public static string GetDescriptorColumn()
        {
            return "P_status";
        }
        
        #region ' Foreign Keys '
        public IQueryable<StatusPedido> StatusPedidos
        {
            get
            {
                
                  var repo=Domain.Model.StatusPedido.GetRepo();
                  return from items in repo.GetAll()
                       where items.Id_Status == _Id_Status
                       select items;
            }
        }

        #endregion
        

        int _Id_Status;
        public int Id_Status
        {
            get { return _Id_Status; }
            set
            {
                if(_Id_Status!=value){
                    _Id_Status=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Id_Status");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _P_status;
        public string P_status
        {
            get { return _P_status; }
            set
            {
                if(_P_status!=value){
                    _P_status=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="P_status");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
		
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<Status, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }

		public void GeraAvaliacoes()
        {
            DbCommand cmd = _db.DataProvider.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_tempoAvaliacao";
            cmd.Connection = _db.DataProvider.CurrentSharedConnection;

            int r = cmd.ExecuteNonQuery();

            cmd.Dispose();
        }
        
		public void ReativaEmails()
        {
            DbCommand cmd = _db.DataProvider.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_reativaEmails";
            cmd.Connection = _db.DataProvider.CurrentSharedConnection;

            int r = cmd.ExecuteNonQuery();

            cmd.Dispose();
        }

    } 
    
    
    /// <summary>
    /// A class which represents the StatusPedido table in the BelDecor Database.
    /// </summary>
    public partial class StatusPedido: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<StatusPedido> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<StatusPedido>(new Domain.Model.BelDecorDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<StatusPedido> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(StatusPedido item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                StatusPedido item=new StatusPedido();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<StatusPedido> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Domain.Model.BelDecorDB _db;
        public StatusPedido(string connectionString, string providerName) {

            _db=new Domain.Model.BelDecorDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                StatusPedido.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<StatusPedido>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public StatusPedido(){
             _db=new Domain.Model.BelDecorDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public StatusPedido(Expression<Func<StatusPedido, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<StatusPedido> GetRepo(string connectionString, string providerName){
            Domain.Model.BelDecorDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Domain.Model.BelDecorDB();
            }else{
                db=new Domain.Model.BelDecorDB(connectionString, providerName);
            }
            IRepository<StatusPedido> _repo;
            
            if(db.TestMode){
                StatusPedido.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<StatusPedido>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<StatusPedido> GetRepo(){
            return GetRepo("","");
        }
        
        public static StatusPedido SingleOrDefault(Expression<Func<StatusPedido, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            StatusPedido single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static StatusPedido SingleOrDefault(Expression<Func<StatusPedido, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            StatusPedido single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<StatusPedido, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<StatusPedido, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<StatusPedido> Find(Expression<Func<StatusPedido, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<StatusPedido> Find(Expression<Func<StatusPedido, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<StatusPedido> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<StatusPedido> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<StatusPedido> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<StatusPedido> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<StatusPedido> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<StatusPedido> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "Id_StatusPedido";
        }

        public object KeyValue()
        {
            return this.Id_StatusPedido;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.motivo.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(StatusPedido)){
                StatusPedido compare=(StatusPedido)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        //public override int GetHashCode() {
        //    return this.Id_StatusPedido;
        //}
        
        public string DescriptorValue()
        {
                            return this.motivo.ToString();
                    }

        public string DescriptorColumn() {
            return "motivo";
        }
        public static string GetKeyColumn()
        {
            return "Id_StatusPedido";
        }        
        public static string GetDescriptorColumn()
        {
            return "motivo";
        }
        
        #region ' Foreign Keys '
        public IQueryable<Pedido> Pedidos
        {
            get
            {
                
                  var repo=Domain.Model.Pedido.GetRepo();
                  return from items in repo.GetAll()
                       where items.Id_Pedido == _Id_Pedido
                       select items;
            }
        }

        public IQueryable<Status> Statuses
        {
            get
            {
                
                  var repo=Domain.Model.Status.GetRepo();
                  return from items in repo.GetAll()
                       where items.Id_Status == _Id_Status
                       select items;
            }
        }

        #endregion
        

        int _Id_StatusPedido;
        public int Id_StatusPedido
        {
            get { return _Id_StatusPedido; }
            set
            {
                if(_Id_StatusPedido!=value){
                    _Id_StatusPedido=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Id_StatusPedido");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        int? _Id_Status;
        public int? Id_Status
        {
            get { return _Id_Status; }
            set
            {
                if(_Id_Status!=value){
                    _Id_Status=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Id_Status");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        int? _Id_Pedido;
        public int? Id_Pedido
        {
            get { return _Id_Pedido; }
            set
            {
                if(_Id_Pedido!=value){
                    _Id_Pedido=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Id_Pedido");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        DateTime _data;
        public DateTime data
        {
            get { return _data; }
            set
            {
                if(_data!=value){
                    _data=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="data");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _motivo;
        public string motivo
        {
            get { return _motivo; }
            set
            {
                if(_motivo!=value){
                    _motivo=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="motivo");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
		
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<StatusPedido, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }

		public void GeraAvaliacoes()
        {
            DbCommand cmd = _db.DataProvider.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_tempoAvaliacao";
            cmd.Connection = _db.DataProvider.CurrentSharedConnection;

            int r = cmd.ExecuteNonQuery();

            cmd.Dispose();
        }
        
		public void ReativaEmails()
        {
            DbCommand cmd = _db.DataProvider.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_reativaEmails";
            cmd.Connection = _db.DataProvider.CurrentSharedConnection;

            int r = cmd.ExecuteNonQuery();

            cmd.Dispose();
        }

    } 
    
    
    /// <summary>
    /// A class which represents the Telefone table in the BelDecor Database.
    /// </summary>
    public partial class Telefone: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<Telefone> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<Telefone>(new Domain.Model.BelDecorDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<Telefone> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(Telefone item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                Telefone item=new Telefone();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<Telefone> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Domain.Model.BelDecorDB _db;
        public Telefone(string connectionString, string providerName) {

            _db=new Domain.Model.BelDecorDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                Telefone.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<Telefone>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public Telefone(){
             _db=new Domain.Model.BelDecorDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public Telefone(Expression<Func<Telefone, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<Telefone> GetRepo(string connectionString, string providerName){
            Domain.Model.BelDecorDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Domain.Model.BelDecorDB();
            }else{
                db=new Domain.Model.BelDecorDB(connectionString, providerName);
            }
            IRepository<Telefone> _repo;
            
            if(db.TestMode){
                Telefone.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<Telefone>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<Telefone> GetRepo(){
            return GetRepo("","");
        }
        
        public static Telefone SingleOrDefault(Expression<Func<Telefone, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            Telefone single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static Telefone SingleOrDefault(Expression<Func<Telefone, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            Telefone single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<Telefone, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<Telefone, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<Telefone> Find(Expression<Func<Telefone, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<Telefone> Find(Expression<Func<Telefone, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<Telefone> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<Telefone> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<Telefone> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<Telefone> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<Telefone> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<Telefone> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "Id_Telefone";
        }

        public object KeyValue()
        {
            return this.Id_Telefone;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.ddd_fixo.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(Telefone)){
                Telefone compare=(Telefone)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        //public override int GetHashCode() {
        //    return this.Id_Telefone;
        //}
        
        public string DescriptorValue()
        {
                            return this.ddd_fixo.ToString();
                    }

        public string DescriptorColumn() {
            return "ddd_fixo";
        }
        public static string GetKeyColumn()
        {
            return "Id_Telefone";
        }        
        public static string GetDescriptorColumn()
        {
            return "ddd_fixo";
        }
        
        #region ' Foreign Keys '
        public IQueryable<Fornecedor> Fornecedors
        {
            get
            {
                
                  var repo=Domain.Model.Fornecedor.GetRepo();
                  return from items in repo.GetAll()
                       where items.IdTelefone == _Id_Telefone
                       select items;
            }
        }

        public IQueryable<Pessoa> Pessoas
        {
            get
            {
                
                  var repo=Domain.Model.Pessoa.GetRepo();
                  return from items in repo.GetAll()
                       where items.IdTelefone == _Id_Telefone
                       select items;
            }
        }

        #endregion
        

        int _Id_Telefone;
        public int Id_Telefone
        {
            get { return _Id_Telefone; }
            set
            {
                if(_Id_Telefone!=value){
                    _Id_Telefone=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Id_Telefone");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _ddd_fixo;
        public string ddd_fixo
        {
            get { return _ddd_fixo; }
            set
            {
                if(_ddd_fixo!=value){
                    _ddd_fixo=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ddd_fixo");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _Numero_Fixo;
        public string Numero_Fixo
        {
            get { return _Numero_Fixo; }
            set
            {
                if(_Numero_Fixo!=value){
                    _Numero_Fixo=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Numero_Fixo");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _Ddd_Cel;
        public string Ddd_Cel
        {
            get { return _Ddd_Cel; }
            set
            {
                if(_Ddd_Cel!=value){
                    _Ddd_Cel=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Ddd_Cel");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _celular;
        public string celular
        {
            get { return _celular; }
            set
            {
                if(_celular!=value){
                    _celular=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="celular");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
		
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<Telefone, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }

		public void GeraAvaliacoes()
        {
            DbCommand cmd = _db.DataProvider.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_tempoAvaliacao";
            cmd.Connection = _db.DataProvider.CurrentSharedConnection;

            int r = cmd.ExecuteNonQuery();

            cmd.Dispose();
        }
        
		public void ReativaEmails()
        {
            DbCommand cmd = _db.DataProvider.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_reativaEmails";
            cmd.Connection = _db.DataProvider.CurrentSharedConnection;

            int r = cmd.ExecuteNonQuery();

            cmd.Dispose();
        }

    } 
    
    
    /// <summary>
    /// A class which represents the TipoConta table in the BelDecor Database.
    /// </summary>
    public partial class TipoConta: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<TipoConta> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<TipoConta>(new Domain.Model.BelDecorDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<TipoConta> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(TipoConta item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                TipoConta item=new TipoConta();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<TipoConta> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Domain.Model.BelDecorDB _db;
        public TipoConta(string connectionString, string providerName) {

            _db=new Domain.Model.BelDecorDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                TipoConta.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<TipoConta>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public TipoConta(){
             _db=new Domain.Model.BelDecorDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public TipoConta(Expression<Func<TipoConta, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<TipoConta> GetRepo(string connectionString, string providerName){
            Domain.Model.BelDecorDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Domain.Model.BelDecorDB();
            }else{
                db=new Domain.Model.BelDecorDB(connectionString, providerName);
            }
            IRepository<TipoConta> _repo;
            
            if(db.TestMode){
                TipoConta.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<TipoConta>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<TipoConta> GetRepo(){
            return GetRepo("","");
        }
        
        public static TipoConta SingleOrDefault(Expression<Func<TipoConta, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            TipoConta single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static TipoConta SingleOrDefault(Expression<Func<TipoConta, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            TipoConta single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<TipoConta, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<TipoConta, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<TipoConta> Find(Expression<Func<TipoConta, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<TipoConta> Find(Expression<Func<TipoConta, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<TipoConta> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<TipoConta> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<TipoConta> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<TipoConta> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<TipoConta> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<TipoConta> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "idTipoConta";
        }

        public object KeyValue()
        {
            return this.idTipoConta;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.Descricao.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(TipoConta)){
                TipoConta compare=(TipoConta)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        //public override int GetHashCode() {
        //    return this.idTipoConta;
        //}
        
        public string DescriptorValue()
        {
                            return this.Descricao.ToString();
                    }

        public string DescriptorColumn() {
            return "Descricao";
        }
        public static string GetKeyColumn()
        {
            return "idTipoConta";
        }        
        public static string GetDescriptorColumn()
        {
            return "Descricao";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _idTipoConta;
        public int idTipoConta
        {
            get { return _idTipoConta; }
            set
            {
                if(_idTipoConta!=value){
                    _idTipoConta=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="idTipoConta");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _Descricao;
        public string Descricao
        {
            get { return _Descricao; }
            set
            {
                if(_Descricao!=value){
                    _Descricao=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Descricao");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
		
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<TipoConta, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }

		public void GeraAvaliacoes()
        {
            DbCommand cmd = _db.DataProvider.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_tempoAvaliacao";
            cmd.Connection = _db.DataProvider.CurrentSharedConnection;

            int r = cmd.ExecuteNonQuery();

            cmd.Dispose();
        }
        
		public void ReativaEmails()
        {
            DbCommand cmd = _db.DataProvider.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_reativaEmails";
            cmd.Connection = _db.DataProvider.CurrentSharedConnection;

            int r = cmd.ExecuteNonQuery();

            cmd.Dispose();
        }

    } 
    
    
    /// <summary>
    /// A class which represents the VwCliente table in the BelDecor Database.
    /// </summary>
    public partial class VwCliente: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<VwCliente> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<VwCliente>(new Domain.Model.BelDecorDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<VwCliente> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(VwCliente item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                VwCliente item=new VwCliente();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<VwCliente> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Domain.Model.BelDecorDB _db;
        public VwCliente(string connectionString, string providerName) {

            _db=new Domain.Model.BelDecorDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                VwCliente.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<VwCliente>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public VwCliente(){
             _db=new Domain.Model.BelDecorDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public VwCliente(Expression<Func<VwCliente, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<VwCliente> GetRepo(string connectionString, string providerName){
            Domain.Model.BelDecorDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Domain.Model.BelDecorDB();
            }else{
                db=new Domain.Model.BelDecorDB(connectionString, providerName);
            }
            IRepository<VwCliente> _repo;
            
            if(db.TestMode){
                VwCliente.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<VwCliente>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<VwCliente> GetRepo(){
            return GetRepo("","");
        }
        
        public static VwCliente SingleOrDefault(Expression<Func<VwCliente, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            VwCliente single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static VwCliente SingleOrDefault(Expression<Func<VwCliente, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            VwCliente single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<VwCliente, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<VwCliente, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<VwCliente> Find(Expression<Func<VwCliente, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<VwCliente> Find(Expression<Func<VwCliente, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<VwCliente> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<VwCliente> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<VwCliente> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<VwCliente> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<VwCliente> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<VwCliente> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "id";
        }

        public object KeyValue()
        {
            return this.id;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.nome.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(VwCliente)){
                VwCliente compare=(VwCliente)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        //public override int GetHashCode() {
        //    return this.id;
        //}
        
        public string DescriptorValue()
        {
                            return this.nome.ToString();
                    }

        public string DescriptorColumn() {
            return "nome";
        }
        public static string GetKeyColumn()
        {
            return "id";
        }        
        public static string GetDescriptorColumn()
        {
            return "nome";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _id;
        public int id
        {
            get { return _id; }
            set
            {
                if(_id!=value){
                    _id=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        DateTime? _dataCadastro;
        public DateTime? dataCadastro
        {
            get { return _dataCadastro; }
            set
            {
                if(_dataCadastro!=value){
                    _dataCadastro=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="dataCadastro");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        int? _IdPessoa;
        public int? IdPessoa
        {
            get { return _IdPessoa; }
            set
            {
                if(_IdPessoa!=value){
                    _IdPessoa=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="IdPessoa");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _nome;
        public string nome
        {
            get { return _nome; }
            set
            {
                if(_nome!=value){
                    _nome=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="nome");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _email;
        public string email
        {
            get { return _email; }
            set
            {
                if(_email!=value){
                    _email=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="email");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _sexo;
        public string sexo
        {
            get { return _sexo; }
            set
            {
                if(_sexo!=value){
                    _sexo=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="sexo");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        DateTime? _dataNascimento;
        public DateTime? dataNascimento
        {
            get { return _dataNascimento; }
            set
            {
                if(_dataNascimento!=value){
                    _dataNascimento=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="dataNascimento");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _cpf;
        public string cpf
        {
            get { return _cpf; }
            set
            {
                if(_cpf!=value){
                    _cpf=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="cpf");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _cnpj;
        public string cnpj
        {
            get { return _cnpj; }
            set
            {
                if(_cnpj!=value){
                    _cnpj=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="cnpj");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _rg;
        public string rg
        {
            get { return _rg; }
            set
            {
                if(_rg!=value){
                    _rg=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="rg");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        int? _idEndereco;
        public int? idEndereco
        {
            get { return _idEndereco; }
            set
            {
                if(_idEndereco!=value){
                    _idEndereco=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="idEndereco");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _ddd_fixo;
        public string ddd_fixo
        {
            get { return _ddd_fixo; }
            set
            {
                if(_ddd_fixo!=value){
                    _ddd_fixo=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ddd_fixo");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _Numero_Fixo;
        public string Numero_Fixo
        {
            get { return _Numero_Fixo; }
            set
            {
                if(_Numero_Fixo!=value){
                    _Numero_Fixo=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Numero_Fixo");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _Ddd_Cel;
        public string Ddd_Cel
        {
            get { return _Ddd_Cel; }
            set
            {
                if(_Ddd_Cel!=value){
                    _Ddd_Cel=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Ddd_Cel");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _celular;
        public string celular
        {
            get { return _celular; }
            set
            {
                if(_celular!=value){
                    _celular=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="celular");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _logradouro;
        public string logradouro
        {
            get { return _logradouro; }
            set
            {
                if(_logradouro!=value){
                    _logradouro=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="logradouro");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _numero;
        public string numero
        {
            get { return _numero; }
            set
            {
                if(_numero!=value){
                    _numero=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="numero");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _complemento;
        public string complemento
        {
            get { return _complemento; }
            set
            {
                if(_complemento!=value){
                    _complemento=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="complemento");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _bairro;
        public string bairro
        {
            get { return _bairro; }
            set
            {
                if(_bairro!=value){
                    _bairro=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="bairro");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _cidade;
        public string cidade
        {
            get { return _cidade; }
            set
            {
                if(_cidade!=value){
                    _cidade=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="cidade");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _estado;
        public string estado
        {
            get { return _estado; }
            set
            {
                if(_estado!=value){
                    _estado=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="estado");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _cep;
        public string cep
        {
            get { return _cep; }
            set
            {
                if(_cep!=value){
                    _cep=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="cep");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _pais;
        public string pais
        {
            get { return _pais; }
            set
            {
                if(_pais!=value){
                    _pais=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="pais");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
		
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<VwCliente, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }

		public void GeraAvaliacoes()
        {
            DbCommand cmd = _db.DataProvider.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_tempoAvaliacao";
            cmd.Connection = _db.DataProvider.CurrentSharedConnection;

            int r = cmd.ExecuteNonQuery();

            cmd.Dispose();
        }
        
		public void ReativaEmails()
        {
            DbCommand cmd = _db.DataProvider.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_reativaEmails";
            cmd.Connection = _db.DataProvider.CurrentSharedConnection;

            int r = cmd.ExecuteNonQuery();

            cmd.Dispose();
        }

    } 
    
    
    /// <summary>
    /// A class which represents the VwContaReceber table in the BelDecor Database.
    /// </summary>
    public partial class VwContaReceber: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<VwContaReceber> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<VwContaReceber>(new Domain.Model.BelDecorDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<VwContaReceber> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(VwContaReceber item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                VwContaReceber item=new VwContaReceber();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<VwContaReceber> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Domain.Model.BelDecorDB _db;
        public VwContaReceber(string connectionString, string providerName) {

            _db=new Domain.Model.BelDecorDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                VwContaReceber.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<VwContaReceber>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public VwContaReceber(){
             _db=new Domain.Model.BelDecorDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public VwContaReceber(Expression<Func<VwContaReceber, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<VwContaReceber> GetRepo(string connectionString, string providerName){
            Domain.Model.BelDecorDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Domain.Model.BelDecorDB();
            }else{
                db=new Domain.Model.BelDecorDB(connectionString, providerName);
            }
            IRepository<VwContaReceber> _repo;
            
            if(db.TestMode){
                VwContaReceber.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<VwContaReceber>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<VwContaReceber> GetRepo(){
            return GetRepo("","");
        }
        
        public static VwContaReceber SingleOrDefault(Expression<Func<VwContaReceber, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            VwContaReceber single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static VwContaReceber SingleOrDefault(Expression<Func<VwContaReceber, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            VwContaReceber single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<VwContaReceber, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<VwContaReceber, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<VwContaReceber> Find(Expression<Func<VwContaReceber, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<VwContaReceber> Find(Expression<Func<VwContaReceber, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<VwContaReceber> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<VwContaReceber> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<VwContaReceber> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<VwContaReceber> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<VwContaReceber> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<VwContaReceber> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "Id_Conta";
        }

        public object KeyValue()
        {
            return this.Id_Conta;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.Forma_Recebimento.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(VwContaReceber)){
                VwContaReceber compare=(VwContaReceber)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        //public override int GetHashCode() {
        //    return this.Id_Conta;
        //}
        
        public string DescriptorValue()
        {
                            return this.Forma_Recebimento.ToString();
                    }

        public string DescriptorColumn() {
            return "Forma_Recebimento";
        }
        public static string GetKeyColumn()
        {
            return "Id_Conta";
        }        
        public static string GetDescriptorColumn()
        {
            return "Forma_Recebimento";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _Id_Conta;
        public int Id_Conta
        {
            get { return _Id_Conta; }
            set
            {
                if(_Id_Conta!=value){
                    _Id_Conta=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Id_Conta");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        int? _Id_Pedido;
        public int? Id_Pedido
        {
            get { return _Id_Pedido; }
            set
            {
                if(_Id_Pedido!=value){
                    _Id_Pedido=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Id_Pedido");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _Forma_Recebimento;
        public string Forma_Recebimento
        {
            get { return _Forma_Recebimento; }
            set
            {
                if(_Forma_Recebimento!=value){
                    _Forma_Recebimento=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Forma_Recebimento");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        decimal? _Valor_Recebimento;
        public decimal? Valor_Recebimento
        {
            get { return _Valor_Recebimento; }
            set
            {
                if(_Valor_Recebimento!=value){
                    _Valor_Recebimento=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Valor_Recebimento");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        DateTime? _data_recebimento;
        public DateTime? data_recebimento
        {
            get { return _data_recebimento; }
            set
            {
                if(_data_recebimento!=value){
                    _data_recebimento=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="data_recebimento");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _CodigoPedido;
        public string CodigoPedido
        {
            get { return _CodigoPedido; }
            set
            {
                if(_CodigoPedido!=value){
                    _CodigoPedido=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="CodigoPedido");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        DateTime? _dataCadastro;
        public DateTime? dataCadastro
        {
            get { return _dataCadastro; }
            set
            {
                if(_dataCadastro!=value){
                    _dataCadastro=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="dataCadastro");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        DateTime? _dataVencimento;
        public DateTime? dataVencimento
        {
            get { return _dataVencimento; }
            set
            {
                if(_dataVencimento!=value){
                    _dataVencimento=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="dataVencimento");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        int? _IdCliente;
        public int? IdCliente
        {
            get { return _IdCliente; }
            set
            {
                if(_IdCliente!=value){
                    _IdCliente=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="IdCliente");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _observacoes;
        public string observacoes
        {
            get { return _observacoes; }
            set
            {
                if(_observacoes!=value){
                    _observacoes=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="observacoes");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        decimal? _ValorTotal;
        public decimal? ValorTotal
        {
            get { return _ValorTotal; }
            set
            {
                if(_ValorTotal!=value){
                    _ValorTotal=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ValorTotal");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        bool? _PagamentoEfetuado;
        public bool? PagamentoEfetuado
        {
            get { return _PagamentoEfetuado; }
            set
            {
                if(_PagamentoEfetuado!=value){
                    _PagamentoEfetuado=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="PagamentoEfetuado");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _PgEfetuado;
        public string PgEfetuado
        {
            get { return _PgEfetuado; }
            set
            {
                if(_PgEfetuado!=value){
                    _PgEfetuado=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="PgEfetuado");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _nome;
        public string nome
        {
            get { return _nome; }
            set
            {
                if(_nome!=value){
                    _nome=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="nome");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
		
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<VwContaReceber, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }

		public void GeraAvaliacoes()
        {
            DbCommand cmd = _db.DataProvider.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_tempoAvaliacao";
            cmd.Connection = _db.DataProvider.CurrentSharedConnection;

            int r = cmd.ExecuteNonQuery();

            cmd.Dispose();
        }
        
		public void ReativaEmails()
        {
            DbCommand cmd = _db.DataProvider.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_reativaEmails";
            cmd.Connection = _db.DataProvider.CurrentSharedConnection;

            int r = cmd.ExecuteNonQuery();

            cmd.Dispose();
        }

    } 
    
    
    /// <summary>
    /// A class which represents the VwContasAPagar table in the BelDecor Database.
    /// </summary>
    public partial class VwContasAPagar: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<VwContasAPagar> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<VwContasAPagar>(new Domain.Model.BelDecorDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<VwContasAPagar> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(VwContasAPagar item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                VwContasAPagar item=new VwContasAPagar();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<VwContasAPagar> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Domain.Model.BelDecorDB _db;
        public VwContasAPagar(string connectionString, string providerName) {

            _db=new Domain.Model.BelDecorDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                VwContasAPagar.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<VwContasAPagar>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public VwContasAPagar(){
             _db=new Domain.Model.BelDecorDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public VwContasAPagar(Expression<Func<VwContasAPagar, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<VwContasAPagar> GetRepo(string connectionString, string providerName){
            Domain.Model.BelDecorDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Domain.Model.BelDecorDB();
            }else{
                db=new Domain.Model.BelDecorDB(connectionString, providerName);
            }
            IRepository<VwContasAPagar> _repo;
            
            if(db.TestMode){
                VwContasAPagar.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<VwContasAPagar>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<VwContasAPagar> GetRepo(){
            return GetRepo("","");
        }
        
        public static VwContasAPagar SingleOrDefault(Expression<Func<VwContasAPagar, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            VwContasAPagar single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static VwContasAPagar SingleOrDefault(Expression<Func<VwContasAPagar, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            VwContasAPagar single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<VwContasAPagar, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<VwContasAPagar, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<VwContasAPagar> Find(Expression<Func<VwContasAPagar, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<VwContasAPagar> Find(Expression<Func<VwContasAPagar, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<VwContasAPagar> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<VwContasAPagar> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<VwContasAPagar> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<VwContasAPagar> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<VwContasAPagar> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<VwContasAPagar> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "Id_ContaPagar";
        }

        public object KeyValue()
        {
            return this.Id_ContaPagar;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.Favorecido_Cedente.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(VwContasAPagar)){
                VwContasAPagar compare=(VwContasAPagar)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        //public override int GetHashCode() {
        //    return this.Id_ContaPagar;
        //}
        
        public string DescriptorValue()
        {
                            return this.Favorecido_Cedente.ToString();
                    }

        public string DescriptorColumn() {
            return "Favorecido_Cedente";
        }
        public static string GetKeyColumn()
        {
            return "Id_ContaPagar";
        }        
        public static string GetDescriptorColumn()
        {
            return "Favorecido_Cedente";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _Id_ContaPagar;
        public int Id_ContaPagar
        {
            get { return _Id_ContaPagar; }
            set
            {
                if(_Id_ContaPagar!=value){
                    _Id_ContaPagar=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Id_ContaPagar");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        int? _Numero_Conta;
        public int? Numero_Conta
        {
            get { return _Numero_Conta; }
            set
            {
                if(_Numero_Conta!=value){
                    _Numero_Conta=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Numero_Conta");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _Favorecido_Cedente;
        public string Favorecido_Cedente
        {
            get { return _Favorecido_Cedente; }
            set
            {
                if(_Favorecido_Cedente!=value){
                    _Favorecido_Cedente=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Favorecido_Cedente");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        DateTime _Data_Vencimento;
        public DateTime Data_Vencimento
        {
            get { return _Data_Vencimento; }
            set
            {
                if(_Data_Vencimento!=value){
                    _Data_Vencimento=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Data_Vencimento");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        decimal? _Valor_Conta;
        public decimal? Valor_Conta
        {
            get { return _Valor_Conta; }
            set
            {
                if(_Valor_Conta!=value){
                    _Valor_Conta=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Valor_Conta");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _Observacoes;
        public string Observacoes
        {
            get { return _Observacoes; }
            set
            {
                if(_Observacoes!=value){
                    _Observacoes=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Observacoes");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _PagamentoEfetuado;
        public string PagamentoEfetuado
        {
            get { return _PagamentoEfetuado; }
            set
            {
                if(_PagamentoEfetuado!=value){
                    _PagamentoEfetuado=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="PagamentoEfetuado");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        int? _idTipoConta;
        public int? idTipoConta
        {
            get { return _idTipoConta; }
            set
            {
                if(_idTipoConta!=value){
                    _idTipoConta=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="idTipoConta");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _descricao;
        public string descricao
        {
            get { return _descricao; }
            set
            {
                if(_descricao!=value){
                    _descricao=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="descricao");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
		
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<VwContasAPagar, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }

		public void GeraAvaliacoes()
        {
            DbCommand cmd = _db.DataProvider.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_tempoAvaliacao";
            cmd.Connection = _db.DataProvider.CurrentSharedConnection;

            int r = cmd.ExecuteNonQuery();

            cmd.Dispose();
        }
        
		public void ReativaEmails()
        {
            DbCommand cmd = _db.DataProvider.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_reativaEmails";
            cmd.Connection = _db.DataProvider.CurrentSharedConnection;

            int r = cmd.ExecuteNonQuery();

            cmd.Dispose();
        }

    } 
    
    
    /// <summary>
    /// A class which represents the VwFornecedor table in the BelDecor Database.
    /// </summary>
    public partial class VwFornecedor: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<VwFornecedor> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<VwFornecedor>(new Domain.Model.BelDecorDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<VwFornecedor> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(VwFornecedor item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                VwFornecedor item=new VwFornecedor();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<VwFornecedor> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Domain.Model.BelDecorDB _db;
        public VwFornecedor(string connectionString, string providerName) {

            _db=new Domain.Model.BelDecorDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                VwFornecedor.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<VwFornecedor>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public VwFornecedor(){
             _db=new Domain.Model.BelDecorDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public VwFornecedor(Expression<Func<VwFornecedor, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<VwFornecedor> GetRepo(string connectionString, string providerName){
            Domain.Model.BelDecorDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Domain.Model.BelDecorDB();
            }else{
                db=new Domain.Model.BelDecorDB(connectionString, providerName);
            }
            IRepository<VwFornecedor> _repo;
            
            if(db.TestMode){
                VwFornecedor.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<VwFornecedor>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<VwFornecedor> GetRepo(){
            return GetRepo("","");
        }
        
        public static VwFornecedor SingleOrDefault(Expression<Func<VwFornecedor, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            VwFornecedor single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static VwFornecedor SingleOrDefault(Expression<Func<VwFornecedor, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            VwFornecedor single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<VwFornecedor, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<VwFornecedor, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<VwFornecedor> Find(Expression<Func<VwFornecedor, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<VwFornecedor> Find(Expression<Func<VwFornecedor, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<VwFornecedor> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<VwFornecedor> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<VwFornecedor> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<VwFornecedor> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<VwFornecedor> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<VwFornecedor> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "Id_fornecedor";
        }

        public object KeyValue()
        {
            return this.Id_fornecedor;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.nome.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(VwFornecedor)){
                VwFornecedor compare=(VwFornecedor)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        //public override int GetHashCode() {
        //    return this.Id_fornecedor;
        //}
        
        public string DescriptorValue()
        {
                            return this.nome.ToString();
                    }

        public string DescriptorColumn() {
            return "nome";
        }
        public static string GetKeyColumn()
        {
            return "Id_fornecedor";
        }        
        public static string GetDescriptorColumn()
        {
            return "nome";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _Id_fornecedor;
        public int Id_fornecedor
        {
            get { return _Id_fornecedor; }
            set
            {
                if(_Id_fornecedor!=value){
                    _Id_fornecedor=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Id_fornecedor");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _nome;
        public string nome
        {
            get { return _nome; }
            set
            {
                if(_nome!=value){
                    _nome=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="nome");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _Razao_Social;
        public string Razao_Social
        {
            get { return _Razao_Social; }
            set
            {
                if(_Razao_Social!=value){
                    _Razao_Social=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Razao_Social");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _cnpj;
        public string cnpj
        {
            get { return _cnpj; }
            set
            {
                if(_cnpj!=value){
                    _cnpj=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="cnpj");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _cpf;
        public string cpf
        {
            get { return _cpf; }
            set
            {
                if(_cpf!=value){
                    _cpf=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="cpf");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _fax;
        public string fax
        {
            get { return _fax; }
            set
            {
                if(_fax!=value){
                    _fax=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="fax");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _email;
        public string email
        {
            get { return _email; }
            set
            {
                if(_email!=value){
                    _email=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="email");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        int? _IdEndereco;
        public int? IdEndereco
        {
            get { return _IdEndereco; }
            set
            {
                if(_IdEndereco!=value){
                    _IdEndereco=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="IdEndereco");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        int? _IdTelefone;
        public int? IdTelefone
        {
            get { return _IdTelefone; }
            set
            {
                if(_IdTelefone!=value){
                    _IdTelefone=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="IdTelefone");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _faxDDD;
        public string faxDDD
        {
            get { return _faxDDD; }
            set
            {
                if(_faxDDD!=value){
                    _faxDDD=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="faxDDD");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _logradouro;
        public string logradouro
        {
            get { return _logradouro; }
            set
            {
                if(_logradouro!=value){
                    _logradouro=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="logradouro");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _numero;
        public string numero
        {
            get { return _numero; }
            set
            {
                if(_numero!=value){
                    _numero=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="numero");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _complemento;
        public string complemento
        {
            get { return _complemento; }
            set
            {
                if(_complemento!=value){
                    _complemento=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="complemento");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _bairro;
        public string bairro
        {
            get { return _bairro; }
            set
            {
                if(_bairro!=value){
                    _bairro=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="bairro");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _cidade;
        public string cidade
        {
            get { return _cidade; }
            set
            {
                if(_cidade!=value){
                    _cidade=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="cidade");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _estado;
        public string estado
        {
            get { return _estado; }
            set
            {
                if(_estado!=value){
                    _estado=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="estado");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _cep;
        public string cep
        {
            get { return _cep; }
            set
            {
                if(_cep!=value){
                    _cep=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="cep");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _ddd_fixo;
        public string ddd_fixo
        {
            get { return _ddd_fixo; }
            set
            {
                if(_ddd_fixo!=value){
                    _ddd_fixo=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ddd_fixo");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _Numero_Fixo;
        public string Numero_Fixo
        {
            get { return _Numero_Fixo; }
            set
            {
                if(_Numero_Fixo!=value){
                    _Numero_Fixo=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Numero_Fixo");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _Ddd_Cel;
        public string Ddd_Cel
        {
            get { return _Ddd_Cel; }
            set
            {
                if(_Ddd_Cel!=value){
                    _Ddd_Cel=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Ddd_Cel");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _celular;
        public string celular
        {
            get { return _celular; }
            set
            {
                if(_celular!=value){
                    _celular=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="celular");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _pais;
        public string pais
        {
            get { return _pais; }
            set
            {
                if(_pais!=value){
                    _pais=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="pais");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
		
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<VwFornecedor, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }

		public void GeraAvaliacoes()
        {
            DbCommand cmd = _db.DataProvider.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_tempoAvaliacao";
            cmd.Connection = _db.DataProvider.CurrentSharedConnection;

            int r = cmd.ExecuteNonQuery();

            cmd.Dispose();
        }
        
		public void ReativaEmails()
        {
            DbCommand cmd = _db.DataProvider.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_reativaEmails";
            cmd.Connection = _db.DataProvider.CurrentSharedConnection;

            int r = cmd.ExecuteNonQuery();

            cmd.Dispose();
        }

    } 
    
    
    /// <summary>
    /// A class which represents the VwFuncionario table in the BelDecor Database.
    /// </summary>
    public partial class VwFuncionario: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<VwFuncionario> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<VwFuncionario>(new Domain.Model.BelDecorDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<VwFuncionario> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(VwFuncionario item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                VwFuncionario item=new VwFuncionario();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<VwFuncionario> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Domain.Model.BelDecorDB _db;
        public VwFuncionario(string connectionString, string providerName) {

            _db=new Domain.Model.BelDecorDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                VwFuncionario.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<VwFuncionario>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public VwFuncionario(){
             _db=new Domain.Model.BelDecorDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public VwFuncionario(Expression<Func<VwFuncionario, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<VwFuncionario> GetRepo(string connectionString, string providerName){
            Domain.Model.BelDecorDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Domain.Model.BelDecorDB();
            }else{
                db=new Domain.Model.BelDecorDB(connectionString, providerName);
            }
            IRepository<VwFuncionario> _repo;
            
            if(db.TestMode){
                VwFuncionario.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<VwFuncionario>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<VwFuncionario> GetRepo(){
            return GetRepo("","");
        }
        
        public static VwFuncionario SingleOrDefault(Expression<Func<VwFuncionario, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            VwFuncionario single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static VwFuncionario SingleOrDefault(Expression<Func<VwFuncionario, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            VwFuncionario single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<VwFuncionario, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<VwFuncionario, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<VwFuncionario> Find(Expression<Func<VwFuncionario, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<VwFuncionario> Find(Expression<Func<VwFuncionario, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<VwFuncionario> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<VwFuncionario> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<VwFuncionario> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<VwFuncionario> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<VwFuncionario> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<VwFuncionario> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "id";
        }

        public object KeyValue()
        {
            return this.id;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.Matricula.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(VwFuncionario)){
                VwFuncionario compare=(VwFuncionario)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        //public override int GetHashCode() {
        //    return this.id;
        //}
        
        public string DescriptorValue()
        {
                            return this.Matricula.ToString();
                    }

        public string DescriptorColumn() {
            return "Matricula";
        }
        public static string GetKeyColumn()
        {
            return "id";
        }        
        public static string GetDescriptorColumn()
        {
            return "Matricula";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _id;
        public int id
        {
            get { return _id; }
            set
            {
                if(_id!=value){
                    _id=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _Matricula;
        public string Matricula
        {
            get { return _Matricula; }
            set
            {
                if(_Matricula!=value){
                    _Matricula=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Matricula");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        DateTime? _dataCadastro;
        public DateTime? dataCadastro
        {
            get { return _dataCadastro; }
            set
            {
                if(_dataCadastro!=value){
                    _dataCadastro=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="dataCadastro");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _Cargo;
        public string Cargo
        {
            get { return _Cargo; }
            set
            {
                if(_Cargo!=value){
                    _Cargo=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Cargo");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _Senha;
        public string Senha
        {
            get { return _Senha; }
            set
            {
                if(_Senha!=value){
                    _Senha=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Senha");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _login;
        public string login
        {
            get { return _login; }
            set
            {
                if(_login!=value){
                    _login=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="login");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        int? _idPessoa;
        public int? idPessoa
        {
            get { return _idPessoa; }
            set
            {
                if(_idPessoa!=value){
                    _idPessoa=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="idPessoa");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        int? _Perfil;
        public int? Perfil
        {
            get { return _Perfil; }
            set
            {
                if(_Perfil!=value){
                    _Perfil=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Perfil");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _nome;
        public string nome
        {
            get { return _nome; }
            set
            {
                if(_nome!=value){
                    _nome=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="nome");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _email;
        public string email
        {
            get { return _email; }
            set
            {
                if(_email!=value){
                    _email=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="email");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _sexo;
        public string sexo
        {
            get { return _sexo; }
            set
            {
                if(_sexo!=value){
                    _sexo=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="sexo");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        DateTime? _dataNascimento;
        public DateTime? dataNascimento
        {
            get { return _dataNascimento; }
            set
            {
                if(_dataNascimento!=value){
                    _dataNascimento=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="dataNascimento");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _cpf;
        public string cpf
        {
            get { return _cpf; }
            set
            {
                if(_cpf!=value){
                    _cpf=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="cpf");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _rg;
        public string rg
        {
            get { return _rg; }
            set
            {
                if(_rg!=value){
                    _rg=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="rg");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        int? _idEndereco;
        public int? idEndereco
        {
            get { return _idEndereco; }
            set
            {
                if(_idEndereco!=value){
                    _idEndereco=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="idEndereco");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _ddd_fixo;
        public string ddd_fixo
        {
            get { return _ddd_fixo; }
            set
            {
                if(_ddd_fixo!=value){
                    _ddd_fixo=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ddd_fixo");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _Numero_Fixo;
        public string Numero_Fixo
        {
            get { return _Numero_Fixo; }
            set
            {
                if(_Numero_Fixo!=value){
                    _Numero_Fixo=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Numero_Fixo");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _Ddd_Cel;
        public string Ddd_Cel
        {
            get { return _Ddd_Cel; }
            set
            {
                if(_Ddd_Cel!=value){
                    _Ddd_Cel=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Ddd_Cel");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _celular;
        public string celular
        {
            get { return _celular; }
            set
            {
                if(_celular!=value){
                    _celular=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="celular");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _logradouro;
        public string logradouro
        {
            get { return _logradouro; }
            set
            {
                if(_logradouro!=value){
                    _logradouro=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="logradouro");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _numero;
        public string numero
        {
            get { return _numero; }
            set
            {
                if(_numero!=value){
                    _numero=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="numero");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _complemento;
        public string complemento
        {
            get { return _complemento; }
            set
            {
                if(_complemento!=value){
                    _complemento=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="complemento");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _bairro;
        public string bairro
        {
            get { return _bairro; }
            set
            {
                if(_bairro!=value){
                    _bairro=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="bairro");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _cidade;
        public string cidade
        {
            get { return _cidade; }
            set
            {
                if(_cidade!=value){
                    _cidade=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="cidade");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _estado;
        public string estado
        {
            get { return _estado; }
            set
            {
                if(_estado!=value){
                    _estado=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="estado");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _cep;
        public string cep
        {
            get { return _cep; }
            set
            {
                if(_cep!=value){
                    _cep=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="cep");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _PerfilDescricao;
        public string PerfilDescricao
        {
            get { return _PerfilDescricao; }
            set
            {
                if(_PerfilDescricao!=value){
                    _PerfilDescricao=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="PerfilDescricao");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _pais;
        public string pais
        {
            get { return _pais; }
            set
            {
                if(_pais!=value){
                    _pais=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="pais");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
		
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<VwFuncionario, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }

		public void GeraAvaliacoes()
        {
            DbCommand cmd = _db.DataProvider.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_tempoAvaliacao";
            cmd.Connection = _db.DataProvider.CurrentSharedConnection;

            int r = cmd.ExecuteNonQuery();

            cmd.Dispose();
        }
        
		public void ReativaEmails()
        {
            DbCommand cmd = _db.DataProvider.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_reativaEmails";
            cmd.Connection = _db.DataProvider.CurrentSharedConnection;

            int r = cmd.ExecuteNonQuery();

            cmd.Dispose();
        }

    } 
    
    
    /// <summary>
    /// A class which represents the vwPedidos table in the BelDecor Database.
    /// </summary>
    public partial class vwPedido: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<vwPedido> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<vwPedido>(new Domain.Model.BelDecorDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<vwPedido> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(vwPedido item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                vwPedido item=new vwPedido();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<vwPedido> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Domain.Model.BelDecorDB _db;
        public vwPedido(string connectionString, string providerName) {

            _db=new Domain.Model.BelDecorDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                vwPedido.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<vwPedido>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public vwPedido(){
             _db=new Domain.Model.BelDecorDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public vwPedido(Expression<Func<vwPedido, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<vwPedido> GetRepo(string connectionString, string providerName){
            Domain.Model.BelDecorDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Domain.Model.BelDecorDB();
            }else{
                db=new Domain.Model.BelDecorDB(connectionString, providerName);
            }
            IRepository<vwPedido> _repo;
            
            if(db.TestMode){
                vwPedido.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<vwPedido>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<vwPedido> GetRepo(){
            return GetRepo("","");
        }
        
        public static vwPedido SingleOrDefault(Expression<Func<vwPedido, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            vwPedido single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static vwPedido SingleOrDefault(Expression<Func<vwPedido, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            vwPedido single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<vwPedido, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<vwPedido, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<vwPedido> Find(Expression<Func<vwPedido, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<vwPedido> Find(Expression<Func<vwPedido, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<vwPedido> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<vwPedido> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<vwPedido> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<vwPedido> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<vwPedido> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<vwPedido> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "Id_Pedido";
        }

        public object KeyValue()
        {
            return this.Id_Pedido;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.observacoes.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(vwPedido)){
                vwPedido compare=(vwPedido)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        //public override int GetHashCode() {
        //    return this.Id_Pedido;
        //}
        
        public string DescriptorValue()
        {
                            return this.observacoes.ToString();
                    }

        public string DescriptorColumn() {
            return "observacoes";
        }
        public static string GetKeyColumn()
        {
            return "Id_Pedido";
        }        
        public static string GetDescriptorColumn()
        {
            return "observacoes";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _Id_Pedido;
        public int Id_Pedido
        {
            get { return _Id_Pedido; }
            set
            {
                if(_Id_Pedido!=value){
                    _Id_Pedido=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Id_Pedido");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _observacoes;
        public string observacoes
        {
            get { return _observacoes; }
            set
            {
                if(_observacoes!=value){
                    _observacoes=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="observacoes");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        int? _IdCliente;
        public int? IdCliente
        {
            get { return _IdCliente; }
            set
            {
                if(_IdCliente!=value){
                    _IdCliente=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="IdCliente");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        DateTime? _dataCadastro;
        public DateTime? dataCadastro
        {
            get { return _dataCadastro; }
            set
            {
                if(_dataCadastro!=value){
                    _dataCadastro=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="dataCadastro");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        DateTime? _dataVencimento;
        public DateTime? dataVencimento
        {
            get { return _dataVencimento; }
            set
            {
                if(_dataVencimento!=value){
                    _dataVencimento=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="dataVencimento");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        decimal? _ValorTotal;
        public decimal? ValorTotal
        {
            get { return _ValorTotal; }
            set
            {
                if(_ValorTotal!=value){
                    _ValorTotal=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ValorTotal");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _CodigoPedido;
        public string CodigoPedido
        {
            get { return _CodigoPedido; }
            set
            {
                if(_CodigoPedido!=value){
                    _CodigoPedido=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="CodigoPedido");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        bool? _PagamentoEfetuado;
        public bool? PagamentoEfetuado
        {
            get { return _PagamentoEfetuado; }
            set
            {
                if(_PagamentoEfetuado!=value){
                    _PagamentoEfetuado=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="PagamentoEfetuado");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        int? _IdPessoa;
        public int? IdPessoa
        {
            get { return _IdPessoa; }
            set
            {
                if(_IdPessoa!=value){
                    _IdPessoa=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="IdPessoa");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _nome;
        public string nome
        {
            get { return _nome; }
            set
            {
                if(_nome!=value){
                    _nome=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="nome");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _cpf;
        public string cpf
        {
            get { return _cpf; }
            set
            {
                if(_cpf!=value){
                    _cpf=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="cpf");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _rg;
        public string rg
        {
            get { return _rg; }
            set
            {
                if(_rg!=value){
                    _rg=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="rg");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
		
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<vwPedido, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }

		public void GeraAvaliacoes()
        {
            DbCommand cmd = _db.DataProvider.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_tempoAvaliacao";
            cmd.Connection = _db.DataProvider.CurrentSharedConnection;

            int r = cmd.ExecuteNonQuery();

            cmd.Dispose();
        }
        
		public void ReativaEmails()
        {
            DbCommand cmd = _db.DataProvider.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_reativaEmails";
            cmd.Connection = _db.DataProvider.CurrentSharedConnection;

            int r = cmd.ExecuteNonQuery();

            cmd.Dispose();
        }

    } 
    
    
    /// <summary>
    /// A class which represents the VwProduto table in the BelDecor Database.
    /// </summary>
    public partial class VwProduto: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<VwProduto> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<VwProduto>(new Domain.Model.BelDecorDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<VwProduto> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(VwProduto item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                VwProduto item=new VwProduto();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<VwProduto> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Domain.Model.BelDecorDB _db;
        public VwProduto(string connectionString, string providerName) {

            _db=new Domain.Model.BelDecorDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                VwProduto.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<VwProduto>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public VwProduto(){
             _db=new Domain.Model.BelDecorDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public VwProduto(Expression<Func<VwProduto, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<VwProduto> GetRepo(string connectionString, string providerName){
            Domain.Model.BelDecorDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Domain.Model.BelDecorDB();
            }else{
                db=new Domain.Model.BelDecorDB(connectionString, providerName);
            }
            IRepository<VwProduto> _repo;
            
            if(db.TestMode){
                VwProduto.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<VwProduto>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<VwProduto> GetRepo(){
            return GetRepo("","");
        }
        
        public static VwProduto SingleOrDefault(Expression<Func<VwProduto, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            VwProduto single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static VwProduto SingleOrDefault(Expression<Func<VwProduto, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            VwProduto single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<VwProduto, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<VwProduto, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<VwProduto> Find(Expression<Func<VwProduto, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<VwProduto> Find(Expression<Func<VwProduto, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<VwProduto> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<VwProduto> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<VwProduto> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<VwProduto> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<VwProduto> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<VwProduto> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "Id_Produto";
        }

        public object KeyValue()
        {
            return this.Id_Produto;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.nome.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(VwProduto)){
                VwProduto compare=(VwProduto)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        //public override int GetHashCode() {
        //    return this.Id_Produto;
        //}
        
        public string DescriptorValue()
        {
                            return this.nome.ToString();
                    }

        public string DescriptorColumn() {
            return "nome";
        }
        public static string GetKeyColumn()
        {
            return "Id_Produto";
        }        
        public static string GetDescriptorColumn()
        {
            return "nome";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _Id_Produto;
        public int Id_Produto
        {
            get { return _Id_Produto; }
            set
            {
                if(_Id_Produto!=value){
                    _Id_Produto=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Id_Produto");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _nome;
        public string nome
        {
            get { return _nome; }
            set
            {
                if(_nome!=value){
                    _nome=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="nome");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _observacoes;
        public string observacoes
        {
            get { return _observacoes; }
            set
            {
                if(_observacoes!=value){
                    _observacoes=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="observacoes");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _codigoProduto;
        public string codigoProduto
        {
            get { return _codigoProduto; }
            set
            {
                if(_codigoProduto!=value){
                    _codigoProduto=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="codigoProduto");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        int? _IdFornecedor;
        public int? IdFornecedor
        {
            get { return _IdFornecedor; }
            set
            {
                if(_IdFornecedor!=value){
                    _IdFornecedor=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="IdFornecedor");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _FornecedorNome;
        public string FornecedorNome
        {
            get { return _FornecedorNome; }
            set
            {
                if(_FornecedorNome!=value){
                    _FornecedorNome=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="FornecedorNome");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
		
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<VwProduto, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }

		public void GeraAvaliacoes()
        {
            DbCommand cmd = _db.DataProvider.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_tempoAvaliacao";
            cmd.Connection = _db.DataProvider.CurrentSharedConnection;

            int r = cmd.ExecuteNonQuery();

            cmd.Dispose();
        }
        
		public void ReativaEmails()
        {
            DbCommand cmd = _db.DataProvider.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_reativaEmails";
            cmd.Connection = _db.DataProvider.CurrentSharedConnection;

            int r = cmd.ExecuteNonQuery();

            cmd.Dispose();
        }

    } 
    
    
    /// <summary>
    /// A class which represents the VwProdutoPedido table in the BelDecor Database.
    /// </summary>
    public partial class VwProdutoPedido: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<VwProdutoPedido> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<VwProdutoPedido>(new Domain.Model.BelDecorDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<VwProdutoPedido> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(VwProdutoPedido item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                VwProdutoPedido item=new VwProdutoPedido();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<VwProdutoPedido> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Domain.Model.BelDecorDB _db;
        public VwProdutoPedido(string connectionString, string providerName) {

            _db=new Domain.Model.BelDecorDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                VwProdutoPedido.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<VwProdutoPedido>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public VwProdutoPedido(){
             _db=new Domain.Model.BelDecorDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public VwProdutoPedido(Expression<Func<VwProdutoPedido, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<VwProdutoPedido> GetRepo(string connectionString, string providerName){
            Domain.Model.BelDecorDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Domain.Model.BelDecorDB();
            }else{
                db=new Domain.Model.BelDecorDB(connectionString, providerName);
            }
            IRepository<VwProdutoPedido> _repo;
            
            if(db.TestMode){
                VwProdutoPedido.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<VwProdutoPedido>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<VwProdutoPedido> GetRepo(){
            return GetRepo("","");
        }
        
        public static VwProdutoPedido SingleOrDefault(Expression<Func<VwProdutoPedido, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            VwProdutoPedido single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static VwProdutoPedido SingleOrDefault(Expression<Func<VwProdutoPedido, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            VwProdutoPedido single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<VwProdutoPedido, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<VwProdutoPedido, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<VwProdutoPedido> Find(Expression<Func<VwProdutoPedido, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<VwProdutoPedido> Find(Expression<Func<VwProdutoPedido, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<VwProdutoPedido> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<VwProdutoPedido> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<VwProdutoPedido> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<VwProdutoPedido> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<VwProdutoPedido> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<VwProdutoPedido> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "Id_ProdutoPedido";
        }

        public object KeyValue()
        {
            return this.Id_ProdutoPedido;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.cor.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(VwProdutoPedido)){
                VwProdutoPedido compare=(VwProdutoPedido)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        //public override int GetHashCode() {
        //    return this.Id_ProdutoPedido;
        //}
        
        public string DescriptorValue()
        {
                            return this.cor.ToString();
                    }

        public string DescriptorColumn() {
            return "cor";
        }
        public static string GetKeyColumn()
        {
            return "Id_ProdutoPedido";
        }        
        public static string GetDescriptorColumn()
        {
            return "cor";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _Id_ProdutoPedido;
        public int Id_ProdutoPedido
        {
            get { return _Id_ProdutoPedido; }
            set
            {
                if(_Id_ProdutoPedido!=value){
                    _Id_ProdutoPedido=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Id_ProdutoPedido");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        int _Id_Produto;
        public int Id_Produto
        {
            get { return _Id_Produto; }
            set
            {
                if(_Id_Produto!=value){
                    _Id_Produto=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Id_Produto");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        int _Id_Pedido;
        public int Id_Pedido
        {
            get { return _Id_Pedido; }
            set
            {
                if(_Id_Pedido!=value){
                    _Id_Pedido=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Id_Pedido");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        int? _Id_Fornecedor;
        public int? Id_Fornecedor
        {
            get { return _Id_Fornecedor; }
            set
            {
                if(_Id_Fornecedor!=value){
                    _Id_Fornecedor=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Id_Fornecedor");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        decimal? _valor;
        public decimal? valor
        {
            get { return _valor; }
            set
            {
                if(_valor!=value){
                    _valor=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="valor");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        decimal? _desconto;
        public decimal? desconto
        {
            get { return _desconto; }
            set
            {
                if(_desconto!=value){
                    _desconto=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="desconto");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        int? _quantidade;
        public int? quantidade
        {
            get { return _quantidade; }
            set
            {
                if(_quantidade!=value){
                    _quantidade=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="quantidade");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        double? _altura;
        public double? altura
        {
            get { return _altura; }
            set
            {
                if(_altura!=value){
                    _altura=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="altura");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        double? _largura;
        public double? largura
        {
            get { return _largura; }
            set
            {
                if(_largura!=value){
                    _largura=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="largura");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        double? _espessura;
        public double? espessura
        {
            get { return _espessura; }
            set
            {
                if(_espessura!=value){
                    _espessura=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="espessura");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        double? _profundidade;
        public double? profundidade
        {
            get { return _profundidade; }
            set
            {
                if(_profundidade!=value){
                    _profundidade=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="profundidade");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _cor;
        public string cor
        {
            get { return _cor; }
            set
            {
                if(_cor!=value){
                    _cor=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="cor");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _NomeProduto;
        public string NomeProduto
        {
            get { return _NomeProduto; }
            set
            {
                if(_NomeProduto!=value){
                    _NomeProduto=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="NomeProduto");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _observacoes;
        public string observacoes
        {
            get { return _observacoes; }
            set
            {
                if(_observacoes!=value){
                    _observacoes=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="observacoes");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _codigoProduto;
        public string codigoProduto
        {
            get { return _codigoProduto; }
            set
            {
                if(_codigoProduto!=value){
                    _codigoProduto=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="codigoProduto");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _NomeFornecedor;
        public string NomeFornecedor
        {
            get { return _NomeFornecedor; }
            set
            {
                if(_NomeFornecedor!=value){
                    _NomeFornecedor=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="NomeFornecedor");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _Razao_Social;
        public string Razao_Social
        {
            get { return _Razao_Social; }
            set
            {
                if(_Razao_Social!=value){
                    _Razao_Social=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Razao_Social");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
		
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<VwProdutoPedido, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }

		public void GeraAvaliacoes()
        {
            DbCommand cmd = _db.DataProvider.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_tempoAvaliacao";
            cmd.Connection = _db.DataProvider.CurrentSharedConnection;

            int r = cmd.ExecuteNonQuery();

            cmd.Dispose();
        }
        
		public void ReativaEmails()
        {
            DbCommand cmd = _db.DataProvider.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_reativaEmails";
            cmd.Connection = _db.DataProvider.CurrentSharedConnection;

            int r = cmd.ExecuteNonQuery();

            cmd.Dispose();
        }

    } 
    
    
    /// <summary>
    /// A class which represents the VwServicoPedido table in the BelDecor Database.
    /// </summary>
    public partial class VwServicoPedido: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<VwServicoPedido> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<VwServicoPedido>(new Domain.Model.BelDecorDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<VwServicoPedido> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(VwServicoPedido item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                VwServicoPedido item=new VwServicoPedido();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<VwServicoPedido> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        Domain.Model.BelDecorDB _db;
        public VwServicoPedido(string connectionString, string providerName) {

            _db=new Domain.Model.BelDecorDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                VwServicoPedido.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<VwServicoPedido>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public VwServicoPedido(){
             _db=new Domain.Model.BelDecorDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public VwServicoPedido(Expression<Func<VwServicoPedido, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<VwServicoPedido> GetRepo(string connectionString, string providerName){
            Domain.Model.BelDecorDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new Domain.Model.BelDecorDB();
            }else{
                db=new Domain.Model.BelDecorDB(connectionString, providerName);
            }
            IRepository<VwServicoPedido> _repo;
            
            if(db.TestMode){
                VwServicoPedido.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<VwServicoPedido>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<VwServicoPedido> GetRepo(){
            return GetRepo("","");
        }
        
        public static VwServicoPedido SingleOrDefault(Expression<Func<VwServicoPedido, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            VwServicoPedido single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static VwServicoPedido SingleOrDefault(Expression<Func<VwServicoPedido, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            VwServicoPedido single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<VwServicoPedido, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<VwServicoPedido, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<VwServicoPedido> Find(Expression<Func<VwServicoPedido, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<VwServicoPedido> Find(Expression<Func<VwServicoPedido, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<VwServicoPedido> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<VwServicoPedido> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<VwServicoPedido> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<VwServicoPedido> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<VwServicoPedido> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<VwServicoPedido> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "Id_SevicoPedido";
        }

        public object KeyValue()
        {
            return this.Id_SevicoPedido;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.descricao.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(VwServicoPedido)){
                VwServicoPedido compare=(VwServicoPedido)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        //public override int GetHashCode() {
        //    return this.Id_SevicoPedido;
        //}
        
        public string DescriptorValue()
        {
                            return this.descricao.ToString();
                    }

        public string DescriptorColumn() {
            return "descricao";
        }
        public static string GetKeyColumn()
        {
            return "Id_SevicoPedido";
        }        
        public static string GetDescriptorColumn()
        {
            return "descricao";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _Id_SevicoPedido;
        public int Id_SevicoPedido
        {
            get { return _Id_SevicoPedido; }
            set
            {
                if(_Id_SevicoPedido!=value){
                    _Id_SevicoPedido=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Id_SevicoPedido");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        int _Id_Servico;
        public int Id_Servico
        {
            get { return _Id_Servico; }
            set
            {
                if(_Id_Servico!=value){
                    _Id_Servico=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Id_Servico");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        int _Id_Pedido;
        public int Id_Pedido
        {
            get { return _Id_Pedido; }
            set
            {
                if(_Id_Pedido!=value){
                    _Id_Pedido=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Id_Pedido");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        decimal? _Desconto;
        public decimal? Desconto
        {
            get { return _Desconto; }
            set
            {
                if(_Desconto!=value){
                    _Desconto=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Desconto");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        decimal? _valor;
        public decimal? valor
        {
            get { return _valor; }
            set
            {
                if(_valor!=value){
                    _valor=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="valor");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _descricao;
        public string descricao
        {
            get { return _descricao; }
            set
            {
                if(_descricao!=value){
                    _descricao=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="descricao");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        decimal? _Preco;
        public decimal? Preco
        {
            get { return _Preco; }
            set
            {
                if(_Preco!=value){
                    _Preco=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Preco");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }

        string _tipo;
        public string tipo
        {
            get { return _tipo; }
            set
            {
                if(_tipo!=value){
                    _tipo=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="tipo");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }

                               
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
		
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<VwServicoPedido, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }

		public void GeraAvaliacoes()
        {
            DbCommand cmd = _db.DataProvider.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_tempoAvaliacao";
            cmd.Connection = _db.DataProvider.CurrentSharedConnection;

            int r = cmd.ExecuteNonQuery();

            cmd.Dispose();
        }
        
		public void ReativaEmails()
        {
            DbCommand cmd = _db.DataProvider.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_reativaEmails";
            cmd.Connection = _db.DataProvider.CurrentSharedConnection;

            int r = cmd.ExecuteNonQuery();

            cmd.Dispose();
        }

    } 
}
